Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Nested For Loop with Else Causes Exception #86

Open
mankyd opened this Issue · 0 comments

1 participant

@mankyd

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 referenced this issue from a commit in njl/jinja2
@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
Something went wrong with that request. Please try again.