Fixing _.template() bug where using 'p' as a variable name would override the variable by the same name in _.template(), causing an error. See test case utilities.namespaceCollisionTemplate() for example of broken case.

This commit is contained in:
Samuel Clay
2010-09-23 12:35:25 -04:00
parent cd93790282
commit 2068f0819d
3 changed files with 16 additions and 4 deletions

1
README
View File

@@ -31,3 +31,4 @@ Many thanks to our contributors:
Luke Sutton
Ryan Tenney
John Wright
Samuel Clay

View File

@@ -58,6 +58,17 @@ $(document).ready(function() {
var fancyTemplate = _.template("<ul><% for (key in people) { %><li><%= people[key] %></li><% } %></ul>");
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
var namespaceCollisionTemplate = _.template("<%= pageCount %> <%= thumbnails[pageCount] %> <% _.each(thumbnails, function(p) { %><div class=\"DV-thumbnail\"><img src=\"<%= p %>\" /></div><% }); %>");
result = namespaceCollisionTemplate({
pageCount: 3,
thumbnails: {
1: "p1-thumbnail.gif",
2: "p2-thumbnail.gif",
3: "p3-thumbnail.gif"
}
});
equals(result, "3 p3-thumbnail.gif <div class=\"DV-thumbnail\"><img src=\"p1-thumbnail.gif\" /></div><div class=\"DV-thumbnail\"><img src=\"p2-thumbnail.gif\" /></div><div class=\"DV-thumbnail\"><img src=\"p3-thumbnail.gif\" /></div>");
var noInterpolateTemplate = _.template("<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");
result = noInterpolateTemplate();

View File

@@ -630,8 +630,8 @@
var c = _.templateSettings;
var endMatch = new RegExp("'(?=[^"+c.end.substr(0, 1)+"]*"+escapeRegExp(c.end)+")","g");
var fn = new Function('obj',
'var p=[],print=function(){p.push.apply(p,arguments);};' +
'with(obj||{}){p.push(\'' +
'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
'with(obj||{}){__p.push(\'' +
str.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
@@ -640,8 +640,8 @@
.split("✄").join("'")
.replace(c.interpolate, "',$1,'")
.split(c.start).join("');")
.split(c.end).join("p.push('")
+ "');}return p.join('');");
.split(c.end).join("__p.push('")
+ "');}return __p.join('');");
return data ? fn(data) : fn;
};