Closed kmck closed 8 years ago
Can you add tests for this?
@SpaceK33z: Sure thing. Updated!
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.
@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!
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
@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);
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 iflodash
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 templaterequire
happens.