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

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();