Skip to content

Commit

Permalink
Attach template source to returned function.
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Mar 23, 2012
1 parent ebb9db4 commit 2055d74
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions underscore.js
Expand Up @@ -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.
Expand Down

0 comments on commit 2055d74

Please sign in to comment.