Browse files

Fix to issue #86, nested loop in a loop else tag

The generated code assumed that l_loop exists inside the if statement
following the for loop. If the LoopContext doesn't have anything,
the l_loop won't be defined, and stuff blows up.
  • Loading branch information...
1 parent 7336b7a commit f6f3704ace00ed52d2b9a46de7614e9c979489c3 @njl committed Mar 12, 2012
Showing with 5 additions and 0 deletions.
  1. +1 −0 jinja2/compiler.py
  2. +4 −0 jinja2/testsuite/core_tags.py
View
1 jinja2/compiler.py
@@ -1068,6 +1068,7 @@ def visit_For(self, node, frame):
# make sure the loop variable is a special one and raise a template
# assertion error if a loop tries to write to loop
if extended_loop:
+ self.writeline('l_loop = None')
loop_frame.identifiers.add_special('loop')
for name in node.find_all(nodes.Name):
if name.ctx == 'store' and name.name == 'loop':
View
4 jinja2/testsuite/core_tags.py
@@ -24,6 +24,10 @@ def test_simple(self):
tmpl = env.from_string('{% for item in seq %}{{ item }}{% endfor %}')
assert tmpl.render(seq=range(10)) == '0123456789'
+ def test_embedded_for_in_else(self):
+ tmpl = env.from_string('{% for item in seq %}{{ loop.index0 }}{% else %}{% for i in range(3) %}{{ i }}{% endfor %}{% endfor %}')
+ assert tmpl.render(seq=[]) == '012'
+
def test_else(self):
tmpl = env.from_string('{% for item in seq %}XXX{% else %}...{% endfor %}')
assert tmpl.render() == '...'

0 comments on commit f6f3704

Please sign in to comment.