emaphp / underscore-template-loader

A Underscore and Lodash template loader for Webpack
MIT License
104 stars 24 forks source link

Optionally enable _.templateSettings.imports #16

Closed kmck closed 8 years ago

kmck commented 8 years ago

Hi there! I took a crack at the two open issues. If you feel good about the approach, I'm happy to add to the tests and update the README.


Issue https://github.com/emaphp/underscore-template-loader/issues/13:

You can specify query.engine to determine which value of _ is used in the template module, which also fixes the issue of the global _ being missing or incorrect, as long as you specify the engine. The part of this that is a little weird is that it necessarily includes the kitchen sink of whatever engine is used.


Issue https://github.com/emaphp/underscore-template-loader/issues/15:

You can specify query.withImports to enable helpers from _.templateSettings.imports to be included in the template automatically. I've enabled this by default if lodash is the engine used. It's using the exact same approach as jstify, so the caveat that _.templateSettings.imports needs to be defined BEFORE the template require happens.

SpaceK33z commented 8 years ago

Can you add tests for this?

kmck commented 8 years ago

@SpaceK33z: Sure thing. Updated!

emaphp commented 8 years ago

I've merged these changes back to master, so the updated package is now available in v0.7.0. The only remaining matter is to add some example in the README reflecting this. I avoided to add it because I'm not entirely familiar of how it worked. @kmck , would you add some minor examples in the docs? I've given @SpaceK33z push privileges in case I'm not around when these changes are made.

SpaceK33z commented 8 years ago

@emaphp, thanks! At my work we're using this package in multiple big projects, so I will keep an eye on the quality :). Hope you'll get well soon!

oller commented 8 years ago

Hi guys,

Enjoying this library. How does one go about importing template settings / helpers on a per template basis?

I have a template loaded in like so:

_.templateSettings.imports = templateHelpers;
var templateTable = require("../../templates/table.html");

However templates referenced by other parts of the application all seem to inherit these templatehelpers, which blocks the globally exposed libraries that I was using in templates elsewhere:

new webpack.ProvidePlugin({
    _: "lodash",
    moment: "moment"
}) //Make these globals available to templates

Any advise suggestions on the best pattern to follow here much appreciated!

Cheers

kmck commented 8 years ago

@oller: If I'm understanding your situation correctly, the global moment probably works, but _ inside the underscore-template-loader module might be overwriting the global _ if you're defining the engine in your underscore-template-loader settings. Does that sound right? Including moment and _ in templateHelpers is more explicit and might be more reliable:

_.templateSettings.imports = _.extend({
    _: lodash, // or just require('lodash') without ProvidePlugin
    moment: moment // or require('moment')
}, templateHelpers);

The whole idea behind _.templateSettings.imports is that they're shared between all templates, so if you need template-specific variables, you're probably better off just passing them in rather than using templateSettings:

var templateTable = require("../../templates/table.html");
templateTable(templateTableHelpers);