From: Armin Ronacher Date: Thu, 14 Aug 2008 10:31:12 +0000 (+0200) Subject: Fixed a bug in the compiler that caused problems with loop not being referenced in... X-Git-Tag: 2.1~43 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=833a3b5d3a1da1ddc3ceabf0ea286ae870e8c41f;p=jinja2.git Fixed a bug in the compiler that caused problems with loop not being referenced in an outer scoped. (Introduced in the last checkin) --HG-- branch : trunk --- diff --git a/jinja2/compiler.py b/jinja2/compiler.py index ad7857d..4c0944a 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -909,7 +909,8 @@ class CodeGenerator(NodeVisitor): # make sure the loop variable is a special one and raise a template # assertion error if a loop tries to write to loop - loop_frame.identifiers.add_special('loop') + if extended_loop: + loop_frame.identifiers.add_special('loop') for name in node.find_all(nodes.Name): if name.ctx == 'store' and name.name == 'loop': self.fail('Can\'t assign to special loop variable ' diff --git a/tests/test_forloop.py b/tests/test_forloop.py index a4f057c..0c307ec 100644 --- a/tests/test_forloop.py +++ b/tests/test_forloop.py @@ -144,3 +144,12 @@ def test_scoped_special_var(env): t = env.from_string('{% for s in seq %}[{{ loop.first }}{% for c in s %}' '|{{ loop.first }}{% endfor %}]{% endfor %}') assert t.render(seq=('ab', 'cd')) == '[True|True|False][False|True|False]' + + +def test_scoped_loop_var(env): + t = env.from_string('{% for x in seq %}{{ loop.first }}' + '{% for y in seq %}{% endfor %}{% endfor %}') + assert t.render(seq='ab') == 'TrueFalse' + t = env.from_string('{% for x in seq %}{% for y in seq %}' + '{{ loop.first }}{% endfor %}{% endfor %}') + assert t.render(seq='ab') == 'TrueFalseTrueFalse'