You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am experiencing an unexpected behavior with Jinja and local variables in a for loop when accessing them from a custom Python context function.
Expected Behavior
I expected for every variable to be available in the context object in a python @contextfunction, even if it is defined/updated in a for loop.
Actual Behavior
The actual behavior is that only variables defined/updated outside the loop are available/have the expected value.
Template Code
Here is a simple template code to demonstrate the behavior:
>>>importjinja2>>>jinja2.__version__'2.9.4'>>>@jinja2.contextfunction
... defmyContextFunction(ctx):
... return"myContextFunction: ctx['myI'] = "+str(ctx['myI'])
...
>>>tmplt="""... {% for i in range(2): %}... {%- set myI = i -%}... {{ myContextFunction() }}... In template myI = {{ myI }}... {% endfor %}... """>>>template=jinja2.Template(tmplt)
>>>template.globals['myContextFunction'] =myContextFunction>>>template.render()
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"/arm/tools/python/jinja2_py2.7.8/2.9.4/rhe6-x86_64/lib/jinja2/environment.py", line1008, inrenderreturnself.environment.handle_exception(exc_info, True)
File"/arm/tools/python/jinja2_py2.7.8/2.9.4/rhe6-x86_64/lib/jinja2/environment.py", line780, inhandle_exceptionreraise(exc_type, exc_value, tb)
File"<template>", line4, intop-leveltemplatecodeFile"<stdin>", line3, inmyContextFunctionFile"/arm/tools/python/jinja2_py2.7.8/2.9.4/rhe6-x86_64/lib/jinja2/runtime.py", line253, in__getitem__raiseKeyError(key)
KeyError: 'myI'>>>context= {'myI' : 0}
>>>printtemplate.render(**context)
myContextFunction: ctx['myI'] =0IntemplatemyI=0myContextFunction: ctx['myI'] =0IntemplatemyI=1
My first goal was to access the i counter, or also the loop.index from my custom contextfunction. Also those variable, I expected to be defined in context, where not available.
Your Environment
Python version: Python 2.7.8
Jinja version: '2.9.4'
The text was updated successfully, but these errors were encountered:
I have a similar problem. Not with looping the variable but simply with accessing the variable set in the template inside the contextfunction's passed context.
Hi,
I am experiencing an unexpected behavior with Jinja and local variables in a for loop when accessing them from a custom Python context function.
Expected Behavior
I expected for every variable to be available in the context object in a python
@contextfunction
, even if it is defined/updated in a for loop.Actual Behavior
The actual behavior is that only variables defined/updated outside the loop are available/have the expected value.
Template Code
Here is a simple template code to demonstrate the behavior:
My first goal was to access the
i
counter, or also theloop.index
from my customcontextfunction
. Also those variable, I expected to be defined in context, where not available.Your Environment
The text was updated successfully, but these errors were encountered: