mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
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:
1
README
1
README
@@ -31,3 +31,4 @@ Many thanks to our contributors:
|
||||
Luke Sutton
|
||||
Ryan Tenney
|
||||
John Wright
|
||||
Samuel Clay
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user