mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 11:57:49 +00:00
Add varname to _.templateSettings.
* Leave out the with statement when using varname. * Rename source to compiled. * Comments, comments, comments.
This commit is contained in:
23
index.html
23
index.html
@@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
|
||||||
<meta http-equiv="X-UA-Compatible" content="chrome=1">
|
<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>
|
<title>Underscore.js</title>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
@@ -1348,6 +1348,7 @@ _.result(object, 'stuff');
|
|||||||
object as the second parameter to <b>template</b> in order to render
|
object as the second parameter to <b>template</b> in order to render
|
||||||
immediately instead of returning a template function.
|
immediately instead of returning a template function.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
var compiled = _.template("hello: <%= name %>");
|
var compiled = _.template("hello: <%= name %>");
|
||||||
compiled({name : 'moe'});
|
compiled({name : 'moe'});
|
||||||
@@ -1393,16 +1394,28 @@ var template = _.template("Hello {{ name }}!");
|
|||||||
template({name : "Mustache"});
|
template({name : "Mustache"});
|
||||||
=> "Hello Mustache!"</pre>
|
=> "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'});
|
||||||
|
=> "no"</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Precompiling your templates can be a big help when debugging errors you can't
|
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
|
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.
|
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.
|
function for easy precompilation.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre><script>
|
<pre><script>
|
||||||
JST.project = <%= _.template(jstText).source %>;
|
JST.project = <%= _.template(jstText).compiled %>;
|
||||||
</script></pre>
|
</script></pre>
|
||||||
|
|
||||||
|
|
||||||
@@ -1570,8 +1583,8 @@ _([1, 2, 3]).value();
|
|||||||
<b class="header">1.2.4</b> — <small><i>Jan. 4, 2012</i></small><br />
|
<b class="header">1.2.4</b> — <small><i>Jan. 4, 2012</i></small><br />
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
You now can (and probably should, as it's simpler)
|
You now can (and probably should, as it's simpler)
|
||||||
write <tt>_.chain(list)</tt>
|
write <tt>_.chain(list)</tt>
|
||||||
instead of <tt>_(list).chain()</tt>.
|
instead of <tt>_(list).chain()</tt>.
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
|
|
||||||
var templateSettings = _.templateSettings;
|
var templateSettings;
|
||||||
|
|
||||||
module("Utility", {
|
module("Utility", {
|
||||||
|
|
||||||
|
setup: function() {
|
||||||
|
templateSettings = _.clone(_.templateSettings);
|
||||||
|
},
|
||||||
|
|
||||||
teardown: function() {
|
teardown: function() {
|
||||||
_.templateSettings = templateSettings;
|
_.templateSettings = templateSettings;
|
||||||
}
|
}
|
||||||
@@ -174,4 +178,9 @@ $(document).ready(function() {
|
|||||||
strictEqual(_.result(null, 'x'), null);
|
strictEqual(_.result(null, 'x'), null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('_.templateSettings.varname', function() {
|
||||||
|
_.templateSettings.varname = 'data';
|
||||||
|
strictEqual(_.template('<%=data.x%>')({x: 'x'}), 'x');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -951,30 +951,43 @@
|
|||||||
// JavaScript micro-templating, similar to John Resig's implementation.
|
// JavaScript micro-templating, similar to John Resig's implementation.
|
||||||
// Underscore templating handles arbitrary delimiters, preserves whitespace,
|
// Underscore templating handles arbitrary delimiters, preserves whitespace,
|
||||||
// and correctly escapes quotes within interpolated code.
|
// and correctly escapes quotes within interpolated code.
|
||||||
_.template = function(str, data) {
|
_.template = function(source, data) {
|
||||||
var settings = _.templateSettings;
|
var settings = _.templateSettings;
|
||||||
var source = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
|
var varname = settings.varname || 'obj';
|
||||||
'with(obj||{}){__p.push(\'' +
|
|
||||||
str
|
// Compile the template source, taking care to escape characters that
|
||||||
.replace(escaper, function(match) {
|
// cannot be included in a string literal and then unescape them in code
|
||||||
return '\\' + escapes[match];
|
// blocks.
|
||||||
})
|
var compiled = "__p.push('" + source
|
||||||
.replace(settings.escape || noMatch, function(match, code) {
|
.replace(escaper, function(match) {
|
||||||
return "',\n_.escape(" + unescape(code) + "),\n'";
|
return '\\' + escapes[match];
|
||||||
})
|
})
|
||||||
.replace(settings.interpolate || noMatch, function(match, code) {
|
.replace(settings.escape || noMatch, function(match, code) {
|
||||||
return "',\n" + unescape(code) + ",\n'";
|
return "',\n_.escape(" + unescape(code) + "),\n'";
|
||||||
})
|
})
|
||||||
.replace(settings.evaluate || noMatch, function(match, code) {
|
.replace(settings.interpolate || noMatch, function(match, code) {
|
||||||
return "');\n" + unescape(code) + "\n;__p.push('";
|
return "',\n" + unescape(code) + ",\n'";
|
||||||
})
|
})
|
||||||
+ "');\n}\nreturn __p.join('');";
|
.replace(settings.evaluate || noMatch, function(match, code) {
|
||||||
var render = new Function('obj', '_', source);
|
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, _);
|
if (data) return render(data, _);
|
||||||
var template = function(data) {
|
var template = function(data) {
|
||||||
return render.call(this, 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;
|
return template;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user