def test_recursive_loop_bug(self): tpl1 = Template(""" {% for p in foo recursive%} {{p.bar}} {% for f in p.fields recursive%} {{f.baz}} {{p.bar}} {% if f.rec %} {{ loop(f.sub) }} {% endif %} {% endfor %} {% endfor %} """) tpl2 = Template(""" {% for p in foo%} {{p.bar}} {% for f in p.fields recursive%} {{f.baz}} {{p.bar}} {% if f.rec %} {{ loop(f.sub) }} {% endif %} {% endfor %} {% endfor %} """)
def test_stopiteration_is_undefined(self): def test(): raise StopIteration() t = Template('A{{ test() }}B') assert t.render(test=test) == 'AB' t = Template('A{{ test().missingattribute }}B') self.assert_raises(UndefinedError, t.render, test=test)
def test_call_with_args(self): t = Template("""{% macro dump_users(users) -%} <ul> {%- for user in users -%} <li><p>{{ user.username|e }}</p>{{ caller(user) }}</li> {%- endfor -%} </ul> {%- endmacro -%} {% call(user) dump_users(list_of_user) -%} <dl> <dl>Realname</dl> <dd>{{ user.realname|e }}</dd> <dl>Description</dl> <dd>{{ user.description }}</dd> </dl> {% endcall %}""") assert [ x.strip() for x in t.render(list_of_user=[{ 'username': '******', 'realname': 'something else', 'description': 'test' }]).splitlines() ] == [ u'<ul><li><p>apo</p><dl>', u'<dl>Realname</dl>', u'<dd>something else</dd>', u'<dl>Description</dl>', u'<dd>test</dd>', u'</dl>', u'</li></ul>' ]
def test_call_with_args(self): t = Template("""{% macro dump_users(users) -%} <ul> {%- for user in users -%} <li><p>{{ user.username|e }}</p>{{ caller(user) }}</li> {%- endfor -%} </ul> {%- endmacro -%} {% call(user) dump_users(list_of_user) -%} <dl> <dl>Realname</dl> <dd>{{ user.realname|e }}</dd> <dl>Description</dl> <dd>{{ user.description }}</dd> </dl> {% endcall %}""") assert [x.strip() for x in t.render(list_of_user=[{ 'username':'******', 'realname':'something else', 'description':'test' }]).splitlines()] == [ u'<ul><li><p>apo</p><dl>', u'<dl>Realname</dl>', u'<dd>something else</dd>', u'<dl>Description</dl>', u'<dd>test</dd>', u'</dl>', u'</li></ul>' ]
def test_stacked_locals_scoping_bug_twoframe(self): t = Template(''' {% set x = 1 %} {% for item in foo %} {% if item == 1 %} {% set x = 2 %} {% endif %} {% endfor %} {{ x }} ''') rv = t.render(foo=[1]).strip() assert rv == u'1'
def test_template_passthrough(self): t = Template('Content') assert env.get_template(t) is t assert env.select_template([t]) is t assert env.get_or_select_template([t]) is t assert env.get_or_select_template(t) is t
def test_indexing_gives_undefined(self): t = Template("{{ var[42].foo }}") self.assert_raises(UndefinedError, t.render, var=0)
def assert_error(code, expected): try: Template(code) except TemplateSyntaxError, e: assert str(e) == expected, 'unexpected error message'