mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Tweak variable names. Update docs.
* Restore template.source. * Use variable instead of varname. * Use text for template input. * Include settings in documentation.
This commit is contained in:
19
index.html
19
index.html
@@ -1335,7 +1335,7 @@ _.result(object, 'stuff');
|
|||||||
=> "nonsense"</pre>
|
=> "nonsense"</pre>
|
||||||
|
|
||||||
<p id="template">
|
<p id="template">
|
||||||
<b class="header">template</b><code>_.template(templateString, [context])</code>
|
<b class="header">template</b><code>_.template(templateString, [data], [settings])</code>
|
||||||
<br />
|
<br />
|
||||||
Compiles JavaScript templates into functions that can be evaluated
|
Compiles JavaScript templates into functions that can be evaluated
|
||||||
for rendering. Useful for rendering complicated bits of HTML from JSON
|
for rendering. Useful for rendering complicated bits of HTML from JSON
|
||||||
@@ -1343,10 +1343,11 @@ _.result(object, 'stuff');
|
|||||||
<tt><%= … %></tt>, as well as execute arbitrary JavaScript code, with
|
<tt><%= … %></tt>, as well as execute arbitrary JavaScript code, with
|
||||||
<tt><% … %></tt>. If you wish to interpolate a value, and have
|
<tt><% … %></tt>. If you wish to interpolate a value, and have
|
||||||
it be HTML-escaped, use <tt><%- … %></tt> When you evaluate a template function, pass in a
|
it be HTML-escaped, use <tt><%- … %></tt> When you evaluate a template function, pass in a
|
||||||
<b>context</b> object that has properties corresponding to the template's free
|
<b>data</b> object that has properties corresponding to the template's free
|
||||||
variables. If you're writing a one-off, you can pass the <b>context</b>
|
variables. If you're writing a one-off, you can pass the <b>data</b>
|
||||||
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. The <b>settings</b> argument
|
||||||
|
should be a hash containing any <tt>_.templateSettings</tt> that should be overriden.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
@@ -1397,25 +1398,23 @@ template({name : "Mustache"});
|
|||||||
<p>
|
<p>
|
||||||
By default, <b>template</b> places the values from your data in the local scope
|
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
|
via the <tt>with</tt> statement. However, you can specify a single variable name
|
||||||
with the <b>varname</b> setting.
|
with the <b>variable</b> setting.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
_.templateSettings.varname = 'data';
|
_.template("<%= data.hasWith %>", {hasWith: 'no'}, {variable: 'data'});
|
||||||
var template = _.template("<%= data.hasWith %>");
|
|
||||||
template({hasWith: 'no'});
|
|
||||||
=> "no"</pre>
|
=> "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>compiled</b> property on the compiled template
|
<b>template</b> provides the <b>source</b> property on the compiled template
|
||||||
function for easy precompilation.
|
function for easy precompilation.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre><script>
|
<pre><script>
|
||||||
JST.project = <%= _.template(jstText).compiled %>;
|
JST.project = <%= _.template(jstText).source %>;
|
||||||
</script></pre>
|
</script></pre>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -178,11 +178,11 @@ $(document).ready(function() {
|
|||||||
strictEqual(_.result(null, 'x'), null);
|
strictEqual(_.result(null, 'x'), null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('_.templateSettings.varname', function() {
|
test('_.templateSettings.variable', function() {
|
||||||
var s = '<%=data.x%>';
|
var s = '<%=data.x%>';
|
||||||
var data = {x: 'x'};
|
var data = {x: 'x'};
|
||||||
strictEqual(_.template(s, data, {varname: 'data'}), 'x')
|
strictEqual(_.template(s, data, {variable: 'data'}), 'x')
|
||||||
_.templateSettings.varname = 'data';
|
_.templateSettings.variable = 'data';
|
||||||
strictEqual(_.template(s)(data), 'x')
|
strictEqual(_.template(s)(data), 'x')
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -951,13 +951,13 @@
|
|||||||
// 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(source, data, settings) {
|
_.template = function(text, data, settings) {
|
||||||
settings = _.extend(_.templateSettings, settings);
|
settings = _.extend(_.templateSettings, settings);
|
||||||
|
|
||||||
// Compile the template source, taking care to escape characters that
|
// Compile the template source, taking care to escape characters that
|
||||||
// cannot be included in a string literal and then unescape them in code
|
// cannot be included in a string literal and then unescape them in code
|
||||||
// blocks.
|
// blocks.
|
||||||
var compiled = "__p.push('" + source
|
var source = "__p.push('" + text
|
||||||
.replace(escaper, function(match) {
|
.replace(escaper, function(match) {
|
||||||
return '\\' + escapes[match];
|
return '\\' + escapes[match];
|
||||||
})
|
})
|
||||||
@@ -971,13 +971,13 @@
|
|||||||
return "');\n" + unescape(code) + "\n;__p.push('";
|
return "');\n" + unescape(code) + "\n;__p.push('";
|
||||||
}) + "');\n";
|
}) + "');\n";
|
||||||
|
|
||||||
// If no varname is specified, place data values in local scope.
|
// If a variable is not specified, place data values in local scope.
|
||||||
if (!settings.varname) compiled = 'with(obj||{}){\n' + compiled + '}\n';
|
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
|
||||||
|
|
||||||
compiled = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};\n' +
|
source = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};\n' +
|
||||||
compiled + "return __p.join('');\n";
|
source + "return __p.join('');\n";
|
||||||
|
|
||||||
var render = new Function(settings.varname || 'obj', '_', compiled);
|
var render = new Function(settings.variable || 'obj', '_', source);
|
||||||
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, _);
|
||||||
@@ -985,8 +985,8 @@
|
|||||||
|
|
||||||
// Provide the compiled function source as a convenience for build time
|
// Provide the compiled function source as a convenience for build time
|
||||||
// precompilation.
|
// precompilation.
|
||||||
template.compiled = 'function(' + (settings.varname || 'obj') + '){\n' +
|
template.source = 'function(' + (settings.variable || 'obj') + '){\n' +
|
||||||
compiled + '\n}';
|
source + '\n}';
|
||||||
|
|
||||||
return template;
|
return template;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user