Add varname to _.templateSettings.

* Leave out the with statement when using varname.
* Rename source to compiled.
* Comments, comments, comments.
This commit is contained in:
Brad Dunbar
2012-04-03 10:57:39 -07:00
parent 8cfb076c9b
commit b04b813f02
3 changed files with 60 additions and 25 deletions

View File

@@ -3,7 +3,7 @@
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<title>Underscore.js</title>
<style>
body {
@@ -1348,6 +1348,7 @@ _.result(object, 'stuff');
object as the second parameter to <b>template</b> in order to render
immediately instead of returning a template function.
</p>
<pre>
var compiled = _.template("hello: &lt;%= name %&gt;");
compiled({name : 'moe'});
@@ -1393,16 +1394,28 @@ var template = _.template("Hello {{ name }}!");
template({name : "Mustache"});
=&gt; "Hello Mustache!"</pre>
<p>
By default, <b>template</b> places the values from your data in the local scope
via the <tt>with</tt> statement. However, you can specify a single variable name
with the <b>varname</b> setting.
</p>
<pre>
_.templateSettings.varname = 'data';
var template = _.template("<%= data.hasWith %>");
template({hasWith: 'no'});
=&gt; "no"</pre>
<p>
Precompiling your templates can be a big help when debugging errors you can't
reproduce. This is because precompiled templates can provide line numbers and
a stack trace, something that is not possible when compiling templates on the client.
<b>template</b> provides the <b>source</b> property on the compiled template
<b>template</b> provides the <b>compiled</b> property on the compiled template
function for easy precompilation.
</p>
<pre>&lt;script&gt;
JST.project = <%= _.template(jstText).source %>;
JST.project = <%= _.template(jstText).compiled %>;
&lt;/script&gt;</pre>
@@ -1570,8 +1583,8 @@ _([1, 2, 3]).value();
<b class="header">1.2.4</b> &mdash; <small><i>Jan. 4, 2012</i></small><br />
<ul>
<li>
You now can (and probably should, as it's simpler)
write <tt>_.chain(list)</tt>
You now can (and probably should, as it's simpler)
write <tt>_.chain(list)</tt>
instead of <tt>_(list).chain()</tt>.
</li>
<li>

View File

@@ -1,9 +1,13 @@
$(document).ready(function() {
var templateSettings = _.templateSettings;
var templateSettings;
module("Utility", {
setup: function() {
templateSettings = _.clone(_.templateSettings);
},
teardown: function() {
_.templateSettings = templateSettings;
}
@@ -174,4 +178,9 @@ $(document).ready(function() {
strictEqual(_.result(null, 'x'), null);
});
test('_.templateSettings.varname', function() {
_.templateSettings.varname = 'data';
strictEqual(_.template('<%=data.x%>')({x: 'x'}), 'x');
});
});

View File

@@ -951,30 +951,43 @@
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
_.template = function(str, data) {
_.template = function(source, data) {
var settings = _.templateSettings;
var source = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
'with(obj||{}){__p.push(\'' +
str
.replace(escaper, function(match) {
return '\\' + escapes[match];
})
.replace(settings.escape || noMatch, function(match, code) {
return "',\n_.escape(" + unescape(code) + "),\n'";
})
.replace(settings.interpolate || noMatch, function(match, code) {
return "',\n" + unescape(code) + ",\n'";
})
.replace(settings.evaluate || noMatch, function(match, code) {
return "');\n" + unescape(code) + "\n;__p.push('";
})
+ "');\n}\nreturn __p.join('');";
var render = new Function('obj', '_', source);
var varname = settings.varname || 'obj';
// Compile the template source, taking care to escape characters that
// cannot be included in a string literal and then unescape them in code
// blocks.
var compiled = "__p.push('" + source
.replace(escaper, function(match) {
return '\\' + escapes[match];
})
.replace(settings.escape || noMatch, function(match, code) {
return "',\n_.escape(" + unescape(code) + "),\n'";
})
.replace(settings.interpolate || noMatch, function(match, code) {
return "',\n" + unescape(code) + ",\n'";
})
.replace(settings.evaluate || noMatch, function(match, code) {
return "');\n" + unescape(code) + "\n;__p.push('";
}) + "');\n";
// If no varname is specified, place data values in local scope.
if (!settings.varname) compiled = 'with(obj||{}){\n' + compiled + '}\n';
compiled = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};\n' +
compiled + "return __p.join('');\n";
var render = new Function(varname, '_', compiled);
if (data) return render(data, _);
var template = function(data) {
return render.call(this, data, _);
};
template.source = 'function(obj){\n' + source + '\n}';
// Provide the compiled function source as a convenience for build time
// precompilation.
template.compiled = 'function(' + varname + '){\n' + compiled + '\n}';
return template;
};