Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jashkenas/underscore
base: c46cd354c2
...
head fork: jashkenas/underscore
compare: 00ed5d70c8
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Showing with 28 additions and 14 deletions.
  1. +13 −1 index.html
  2. +15 −13 underscore.js
View
14 index.html
@@ -1327,7 +1327,7 @@ <h2 id="utility">Utility Functions</h2>
<br />
Compiles JavaScript templates into functions that can be evaluated
for rendering. Useful for rendering complicated bits of HTML from JSON
- data sources. Template functions can both interpolate variables, using<br />
+ data sources. Template functions can both interpolate variables, using
<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
@@ -1381,6 +1381,18 @@ <h2 id="utility">Utility Functions</h2>
template({name : "Mustache"});
=&gt; "Hello Mustache!"</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
+ function for easy precompilation.
+ </p>
+
+ <pre>&lt;script&gt;
+ JST.project = <%= _.template(jstText).source %>;
+&lt;/script&gt;</pre>
+
<h2 id="chaining">Chaining</h2>
View
28 underscore.js
@@ -938,28 +938,30 @@
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
_.template = function(str, data) {
- var c = _.templateSettings;
- var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
+ 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(c.escape || noMatch, function(match, code) {
- return "',_.escape(" + unescape(code) + "),\n'";
+ .replace(settings.escape || noMatch, function(match, code) {
+ return "',\n_.escape(" + unescape(code) + "),\n'";
})
- .replace(c.interpolate || noMatch, function(match, code) {
- return "'," + unescape(code) + ",\n'";
+ .replace(settings.interpolate || noMatch, function(match, code) {
+ return "',\n" + unescape(code) + ",\n'";
})
- .replace(c.evaluate || noMatch, function(match, code) {
- return "');" + unescape(code) + ";\n__p.push('";
+ .replace(settings.evaluate || noMatch, function(match, code) {
+ return "');\n" + unescape(code) + "\n;__p.push('";
})
- + "');}return __p.join('');";
- var func = new Function('obj', '_', tmpl);
- if (data) return func(data, _);
- return function(data) {
- return func.call(this, data, _);
+ + "');\n}\nreturn __p.join('');";
+ var render = new Function('obj', '_', source);
+ if (data) return render(data, _);
+ var template = function(data) {
+ return render.call(this, data, _);
};
+ template.source = 'function(obj){\n' + source + '\n}';
+ return template;
};
// Add a "chain" function, which will delegate to the wrapper.

No commit comments for this range

Something went wrong with that request. Please try again.