Merge branch 'master' of github.com:documentcloud/underscore

This commit is contained in:
Jeremy Ashkenas
2012-04-06 16:54:29 -04:00
3 changed files with 66 additions and 28 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 {
@@ -1335,7 +1335,7 @@ _.result(object, 'stuff');
=&gt; "nonsense"</pre>
<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 />
Compiles JavaScript templates into functions that can be evaluated
for rendering. Useful for rendering complicated bits of HTML from JSON
@@ -1343,11 +1343,13 @@ _.result(object, 'stuff');
<tt>&lt;%= &hellip; %&gt;</tt>, as well as execute arbitrary JavaScript code, with
<tt>&lt;% &hellip; %&gt;</tt>. If you wish to interpolate a value, and have
it be HTML-escaped, use <tt>&lt;%- &hellip; %&gt;</tt> When you evaluate a template function, pass in a
<b>context</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>
<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>data</b>
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>
<pre>
var compiled = _.template("hello: &lt;%= name %&gt;");
compiled({name : 'moe'});
@@ -1393,6 +1395,16 @@ 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>variable</b> setting.
</p>
<pre>
_.template("<%= data.hasWith %>", {hasWith: 'no'}, {variable: 'data'});
=&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
@@ -1570,8 +1582,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,12 @@ $(document).ready(function() {
strictEqual(_.result(null, 'x'), null);
});
test('_.templateSettings.variable', function() {
var s = '<%=data.x%>';
var data = {x: 'x'};
strictEqual(_.template(s, data, {variable: 'data'}), 'x')
_.templateSettings.variable = 'data';
strictEqual(_.template(s)(data), 'x')
});
});

View File

@@ -951,30 +951,44 @@
// 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) {
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);
_.template = function(text, data, settings) {
settings = _.extend(_.templateSettings, settings);
// 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 source = "__p+='" + text
.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+='";
}) + "';\n";
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = "var __p='';" +
"var print=function(){__p+=Array.prototype.join.call(arguments, '')};\n" +
source + "return __p;\n";
var render = new Function(settings.variable || 'obj', '_', source);
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.source = 'function(' + (settings.variable || 'obj') + '){\n' +
source + '}';
return template;
};