Commits

Anonymous committed c859c8e

Fixed missing loop variable in context funcs/filters

Comments (0)

Files changed (5)

jinja2/compiler.py

             loop_frame = frame.inner()
             loop_frame.inspect(children)
 
-        # try to figure out if we have an extended loop.  An extended loop
-        # is necessary if the loop is in recursive mode if the special loop
-        # variable is accessed in the body.
-        extended_loop = node.recursive or 'loop' in \
-                        find_undeclared(node.iter_child_nodes(
-                            only=('body',)), ('loop',))
+        # because the loop variable may be accessed inside of a context
+        # function or filter, we always want an extended loop.
+        extended_loop = True
 
         # if we don't have an recursive loop we have to find the shadowed
         # variables at that point.  Because loops can be nested but the loop
-[egg_info]
-tag_build = dev
-tag_date = true
+;;[egg_info]
+;;tag_build = dev
+;;tag_date = true
 
 [aliases]
 release = egg_info -RDb ''
 
 #: don't change the variable and assignment.  the fabfile parses this
 #: file to get the version for deployment from it.
-VERSION = '2.2'
+VERSION = '2.2.1'
 
 
 data_files = []

tests/test_forloop.py

     {%- for item in foo %}{% set x = item %}{{ 'hello'|xtimes }}{% endfor -%}
     ''')
     assert t.render(foo=(2,)) == 'hellohello'
+
+
+def test_scoping_context_func_loop_available():
+    tmpl = ('''
+    {%- for item in foo %}{{ callme() }}{% endfor -%}
+    ''')
+    print env.compile(tmpl, raw=True)
+    t = env.from_string(tmpl)
+    @contextfunction
+    def callme(context):
+        assert 'item' in context
+        assert 'loop' in context
+        item = context['item']
+        assert item == 1
+        return "%s.%s" % (item, context['loop'].index)
+    assert t.render(foo=(1,), callme=callme) == '1.1'
+
+
+def test_scoping_context_filter_loop_available():
+    @contextfilter
+    def itemtimes(context, arg):
+        assert 'item' in context
+        assert 'loop' in context
+        x = context['item']
+        return arg * (x + context['loop'].index)
+    env.filters['itemtimes'] = itemtimes
+    t = env.from_string('''
+    {%- for item in foo %}{{ 'hello'|itemtimes }}{% endfor -%}
+    ''')
+    assert t.render(foo=(2,)) == 'hellohellohello'

tests/test_old_bugs.py

 
 
 def test_loop_call_loop():
-    tmpl = env.from_string('''
+    tmpl = env.compile('''
 
     {% macro test() %}
         {{ caller() }}
         {% endcall %}
     {% endfor %}
 
-    ''')
-
+    ''', raw=True)
+    print tmpl
     assert tmpl.render() == ''