The underscore _.template uses with to enable things like <%= pancakes %> on obj.pancakes . If you look inside _.template , you will find the following:
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
Where the offensive comes from. If you use precompiled JST style templates, this source is what you get inside your JST object, and that will make with visible within "use strict" . Notice that settings.variable there? The documentation says:
By default, a template places values โโfrom your data in a local area using the with statement. However, you can specify the same variable name with the variable parameter. This can greatly improve the speed with which the template can display.
_.template("Using 'with': <%= data.answer %>", {answer: 'no'}, {variable: 'data'}); => "Using 'with': no"
That way, you can suppress with with the variable parameter when compiling templates; of course, this also means that you will have to rewrite all <%= ... %> parts of your templates according to what the variable parameter can say (this should also speed up your templates so that it can be worth it just for that).
In your case, you change the template to this:
<p><%= data.var1 %><%= data.var2 %>!</p> //<p>Hello World!</p>
and then you will need to change the _.template call, which is used to compile the templates, to look like this:
var compiled_template = _.template(raw_template, null, { variable: 'data' });
You do not need to use data , of course, you just need to use the same thing both in templates and in the _.template call.
I donโt know how you would change your customized _.template calls, but it should not be so difficult. I suppose you could schedule the _.template monkey to have a default value for variable as a last resort.
Here is a simple demo that should illustrate what happens: http://jsfiddle.net/ambiguous/Az8QM/
Alternatively, if we look at how "use strict" has scope , we will see that:
Strict mode applies to all scripts or to individual functions.
So, you can localize your rigor like this:
(function() { "use strict";
You can also use two JavaScript files instead of one:
- One for your non-JavaScript template with
"use strict" included. - The second one with your
JST , this one is not "use strict" .