Nested For Loop with Else Causes Exception #86

Open
mankyd opened this Issue Jan 11, 2012 · 0 comments

Projects

None yet

1 participant

@mankyd
mankyd commented Jan 11, 2012

Create a for loop with an else clause. In the main clause of the for loop, access {{ loop.index }}. In the else clause, put another for loop. When you render the template, you get an exception:

>>> env = jinja2.Environment()
>>> tpl = env.from_string('{% for x in y %}{{ loop.index0 }}{% else %}{% for i in range(3) %}{{ i }}{% endfor %}{% endfor %}')
>>> tpl.render(y=[])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/dave/om/trunk/lib/python2.7/site-packages/Jinja2-2.6-py2.7.egg/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "<template>", line 1, in top-level template code
UnboundLocalError: local variable 'l_loop' referenced before assignment
@njl njl added a commit to njl/jinja2 that referenced this issue Mar 13, 2012
@njl njl 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.
f6f3704
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment