def test_template_variable(self): s = '<%=data["x"]%>' data = {"x": 'x'} self.assertEqual(_.template(s, data, {"variable": 'data'}), 'x') _.templateSettings = { "variable": 'data' } self.assertEqual(_.template(s)(data), 'x')
def test_template_undef(self): template = _.template('<%=x%>') self.assertEqual(template({"x": None}), '') templateEscaped = _.template('<%-x%>') self.assertEqual(templateEscaped({"x": None}), '') templateWithPropertyEscaped = _.template('<%-x["foo"]%>') self.assertEqual(templateWithPropertyEscaped({"x": {"foo": None}}), '')
def test_interpolate_only_once(self): ns = self.Namespace() ns.count = 0 template = _.template('<%= f() %>') def test(): self.assertTrue(not ns.count) ns.count += 1 template({"f": test}) ns.countEscaped = 0 templateEscaped = _.template('<%- f() %>') def test2(): self.assertTrue(not ns.countEscaped) ns.countEscaped += 1 templateEscaped({"f": test2})
def test_template(self): basicTemplate = _.template("<%= thing %> is gettin' on my noives!") result = basicTemplate({"thing": 'This'}) self.assertEqual(result, "This is gettin' on my noives!", 'can do basic attribute interpolation') sansSemicolonTemplate = _.template("A <% this %> B") self.assertEqual(sansSemicolonTemplate(), "A B") backslashTemplate = _.template("<%= thing %> is \ridanculous") self.assertEqual(backslashTemplate({"thing": 'This'}), "This is \ridanculous") escapeTemplate = _.template( '<%= "checked=\\"checked\\"" if a else "" %>') self.assertEqual(escapeTemplate({"a": True}), 'checked="checked"', 'can handle slash escapes in interpolations.') fancyTemplate = _.template( "<ul><% for key in people: %><li><%= key %></li><% endfor %></ul>") result = fancyTemplate({"people": ["Larry", "Curly", "Moe"]}) self.assertEqual(result, "<ul><li>Larry</li><li>Curly</li><li>Moe</li></ul>", 'can run arbitrary javascript in templates') escapedCharsInJavascriptTemplate = _.template( "<ul><% def by(item, *args): %><li><%= item %></li><% enddef %>" "<% _.each(numbers.split('\\n'), by) %></ul>") # print escapedCharsInJavascriptTemplate.source result = escapedCharsInJavascriptTemplate( {"numbers": "one\ntwo\nthree\nfour"}) # print result, "####" self.assertEqual( result, "<ul><li>one</li><li>two</li>" "<li>three</li><li>four</li></ul>", 'Can use escaped characters (e.g. \\n) in Javascript') namespaceCollisionTemplate = _.template( "<%= pageCount %> <%= thumbnails[pageCount] %>" " <% def by(p, *args): %><div class=\"thumbnail\"" " rel=\"<%= p %>\"></div><% enddef %><% _.each(thumbnails, by) %>") result = namespaceCollisionTemplate({ "pageCount": 3, "thumbnails": { 1: "p1-thumbnail.gif", 2: "p2-thumbnail.gif", 3: "p3-thumbnail.gif" } }) self.assertEqual( result, '3 p3-thumbnail.gif <div class="thumbnail"' ' rel="p1-thumbnail.gif"></div><div class="thumbnail"' ' rel="p2-thumbnail.gif"></div><div class="thumbnail"' ' rel="p3-thumbnail.gif"></div>') noInterpolateTemplate = _.template( "<div><p>Just some text. Hey, I know this is silly" " but it aids consistency.</p></div>") result = noInterpolateTemplate() self.assertEqual( result, "<div><p>Just some text. Hey, I know this is" " silly but it aids consistency.</p></div>") quoteTemplate = _.template("It's its, not it's") self.assertEqual(quoteTemplate({}), "It's its, not it's") quoteInStatementAndBody = _.template("<% \ if foo == 'bar': \ %>Statement quotes and 'quotes'.<% endif %>") self.assertEqual(quoteInStatementAndBody({"foo": "bar"}), "Statement quotes and 'quotes'.") withNewlinesAndTabs = _.template( 'This\n\t\tis: <%= x %>.\n\tok.\nend.') self.assertEqual(withNewlinesAndTabs({"x": 'that'}), 'This\n\t\tis: that.\n\tok.\nend.') template = _.template("<i><%- value %></i>") result = template({"value": "<script>"}) self.assertEqual(result, '<i><script></i>') # This wouldn't work in python # stooge = { # "name": "Moe", # "template": _.template("I'm <%= this.name %>") # } # self.assertEqual(stooge.template(), "I'm Moe") _.templateSettings = { "evaluate": r"\{\{([\s\S]+?)\}\}", "interpolate": r"\{\{=([\s\S]+?)\}\}" } custom = _.template( "<ul>{{ for key in people: }}<li>{{= key }}</li>{{ endfor }}</ul>") result = custom({"people": ["Larry", "Curly", "Moe"]}) self.assertEqual(result, "<ul><li>Larry</li><li>Curly</li><li>Moe</li></ul>", 'can run arbitrary javascript in templates') customQuote = _.template("It's its, not it's") self.assertEqual(customQuote({}), "It's its, not it's") quoteInStatementAndBody = _.template( "{{ if foo == 'bar': }}Statement quotes and 'quotes'.{{ endif }}") self.assertEqual(quoteInStatementAndBody({"foo": "bar"}), "Statement quotes and 'quotes'.") _.templateSettings = { "evaluate": r"<\?([\s\S]+?)\?>", "interpolate": r"<\?=([\s\S]+?)\?>" } customWithSpecialChars = _.template( "<ul><? for key in people: ?><li><?= key ?></li><? endfor ?></ul>") result = customWithSpecialChars({"people": ["Larry", "Curly", "Moe"]}) self.assertEqual(result, "<ul><li>Larry</li><li>Curly</li><li>Moe</li></ul>", 'can run arbitrary javascript in templates') customWithSpecialCharsQuote = _.template("It's its, not it's") self.assertEqual(customWithSpecialCharsQuote({}), "It's its, not it's") quoteInStatementAndBody = _.template( "<? if foo == 'bar': ?>Statement quotes and 'quotes'.<? endif ?>") self.assertEqual(quoteInStatementAndBody({"foo": "bar"}), "Statement quotes and 'quotes'.") _.templateSettings = {"interpolate": r"\{\{(.+?)\}\}"} mustache = _.template("Hello {{planet}}!") self.assertEqual(mustache({"planet": "World"}), "Hello World!", "can mimic mustache.js") templateWithNull = _.template("a null undefined {{planet}}") self.assertEqual(templateWithNull({"planet": "world"}), "a null undefined world", "can handle missing escape and evaluate settings")
def test_temp_settings_no_change(self): self.assertFalse("variable" in _.templateSettings) _.template('', {}, {"variable": 'x'}) self.assertFalse("variable" in _.templateSettings)
def test_template_variable(self): s = '<%=data["x"]%>' data = {"x": 'x'} self.assertEqual(_.template(s, data, {"variable": 'data'}), 'x') _.templateSettings = {"variable": 'data'} self.assertEqual(_.template(s)(data), 'x')
def test_template_escape(self): tmpl = _.template('<p>\u2028<%= "\\u2028\\u2029" %>\u2029</p>') self.assertEqual(tmpl(), '<p>\u2028\u2028\u2029\u2029</p>')
def test_template(self): basicTemplate = _.template("<%= thing %> is gettin' on my noives!") result = basicTemplate({"thing": 'This'}) self.assertEqual(result, "This is gettin' on my noives!", 'can do basic attribute interpolation') sansSemicolonTemplate = _.template("A <% this %> B") self.assertEqual(sansSemicolonTemplate(), "A B") backslashTemplate = _.template("<%= thing %> is \\ridanculous") self.assertEqual(backslashTemplate({"thing": 'This'}), "This is \ridanculous") escapeTemplate = _.template('<%= "checked=\\"checked\\"" if a else "" %>') self.assertEqual(escapeTemplate({"a": True}), 'checked="checked"', 'can handle slash escapes in interpolations.') fancyTemplate = _.template("<ul><% for key in people: %><li><%= people[key] %></li><% endfor %></ul>") result = fancyTemplate({"people": {"moe": "Moe", "larry": "Larry", "curly": "Curly"}}) self.assertEqual(result, "<ul><li>Larry</li><li>Curly</li><li>Moe</li></ul>", 'can run arbitrary javascript in templates') escapedCharsInJavascriptTemplate = _.template("<ul><% def by(item, *args): %><li><%= item %></li><% enddef %><% _.each(numbers.split('\\n'), by) %></ul>") #print escapedCharsInJavascriptTemplate.source result = escapedCharsInJavascriptTemplate({"numbers": "one\ntwo\nthree\nfour"}) #print result, "####" self.assertEqual(result, "<ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>", 'Can use escaped characters (e.g. \\n) in Javascript') namespaceCollisionTemplate = _.template("<%= pageCount %> <%= thumbnails[pageCount] %> <% def by(p, *args): %><div class=\"thumbnail\" rel=\"<%= p %>\"></div><% enddef %><% _.each(thumbnails, by) %>") result = namespaceCollisionTemplate({ "pageCount": 3, "thumbnails": { 1: "p1-thumbnail.gif", 2: "p2-thumbnail.gif", 3: "p3-thumbnail.gif" } }) self.assertEqual(result, '3 p3-thumbnail.gif <div class="thumbnail" rel="p1-thumbnail.gif"></div><div class="thumbnail" rel="p2-thumbnail.gif"></div><div class="thumbnail" rel="p3-thumbnail.gif"></div>') noInterpolateTemplate = _.template("<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>") result = noInterpolateTemplate() self.assertEqual(result, "<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>") quoteTemplate = _.template("It's its, not it's") self.assertEqual(quoteTemplate({}), "It's its, not it's") quoteInStatementAndBody = _.template("<% \ if foo == 'bar': \ %>Statement quotes and 'quotes'.<% endif %>") self.assertEqual(quoteInStatementAndBody({"foo": "bar"}), "Statement quotes and 'quotes'.") withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.') self.assertEqual(withNewlinesAndTabs({"x": 'that'}), 'This\n\t\tis: that.\n\tok.\nend.') template = _.template("<i><%- value %></i>") result = template({"value": "<script>"}) self.assertEqual(result, '<i><script></i>') # This wouldn't work in python # stooge = { # "name": "Moe", # "template": _.template("I'm <%= this.name %>") # } # self.assertEqual(stooge.template(), "I'm Moe") _.templateSettings = { "evaluate": r"\{\{([\s\S]+?)\}\}", "interpolate": r"\{\{=([\s\S]+?)\}\}" } custom = _.template("<ul>{{ for key in people: }}<li>{{= people[key] }}</li>{{ endfor }}</ul>") result = custom({"people": {"moe": "Moe", "larry": "Larry", "curly": "Curly"}}) self.assertEqual(result, "<ul><li>Larry</li><li>Curly</li><li>Moe</li></ul>", 'can run arbitrary javascript in templates') customQuote = _.template("It's its, not it's") self.assertEqual(customQuote({}), "It's its, not it's") quoteInStatementAndBody = _.template("{{ if foo == 'bar': }}Statement quotes and 'quotes'.{{ endif }}") self.assertEqual(quoteInStatementAndBody({"foo": "bar"}), "Statement quotes and 'quotes'.") _.templateSettings = { "evaluate": r"<\?([\s\S]+?)\?>", "interpolate": r"<\?=([\s\S]+?)\?>" } customWithSpecialChars = _.template("<ul><? for key in people: ?><li><?= people[key] ?></li><? endfor ?></ul>") result = customWithSpecialChars({"people": {"moe": "Moe", "larry": "Larry", "curly": "Curly"}}) self.assertEqual(result, "<ul><li>Larry</li><li>Curly</li><li>Moe</li></ul>", 'can run arbitrary javascript in templates') customWithSpecialCharsQuote = _.template("It's its, not it's") self.assertEqual(customWithSpecialCharsQuote({}), "It's its, not it's") quoteInStatementAndBody = _.template("<? if foo == 'bar': ?>Statement quotes and 'quotes'.<? endif ?>") self.assertEqual(quoteInStatementAndBody({"foo": "bar"}), "Statement quotes and 'quotes'.") _.templateSettings = { "interpolate": r"\{\{(.+?)\}\}" } mustache = _.template("Hello {{planet}}!") self.assertEqual(mustache({"planet": "World"}), "Hello World!", "can mimic mustache.js") templateWithNull = _.template("a null undefined {{planet}}") self.assertEqual(templateWithNull({"planet": "world"}), "a null undefined world", "can handle missing escape and evaluate settings")