New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
{% set %} doesn't assign var to global context #164
Comments
Chipping in with some more details. This is what the code above looks like to get a feeling for what's happening inside: from jinja2.runtime import LoopContext, TemplateReference, Macro, Markup, TemplateRuntimeError, missing, concat, escape, markup_join, unicode_join, to_string, identity, TemplateNotFound
name = None
def root(context, environment=environment):
...
l_foo = 'bar'
context.vars['foo'] = l_foo
context.exported_vars.add('foo')
yield u'\nfoo - %s\n\n' % (
l_foo,
)
t_1 = l_foo
l_i = missing
for l_i in context.call(l_range, 2):
if 0: yield None
yield u'\n for:foo - %s\n ' % (
l_foo,
)
l_foo = l_i
yield u'\n for:foo - %s\n ' % (
l_foo,
)
l_foo = t_1
l_i = missing
yield u'\n\n'
for event in context.blocks['test'][0](context):
yield event
yield u'\n\nfoo - '
yield to_string(l_foo)
def block_test(context, environment=environment):
l_foo = context.resolve('foo')
if 0: yield None
yield u'\n block:foo - %s\n ' % (
l_foo,
)
l_foo = 'baz'
yield u'\n block:foo - %s\n' % (
l_foo,
)
blocks = {'test': block_test}
debug_info = '2=8&3=12&5=16&6=19&7=21&8=23&11=28&17=31&11=33&12=37&13=39&14=41' More details: Any ideas? |
This is documented behavior. |
mitsuhiko, We have the same problem as DriverX. Our example: {% set the_var = 'foo' %} {% for e in elements %} {% set the_var = 'YES' %} the var: {{ the_var }} {%- endfor %} the_var: {{ the_var }} Result: the_var: YES the_var: YES the_var: foo Can you help us? Thanks |
http://stackoverflow.com/questions/4870346/can-a-jinja-variables-scope-extend-beyond-in-an-inner-block and http://stackoverflow.com/questions/9486393/jinja2-change-the-value-of-a-variable-inside-a-loop I really don't understand why those hacks are neccesary, should be "local var" syntax or similar Regards |
* Update docs/submodules/pharos from branch 'master' - [daisy] network template: Fix set inside loop Previous implementation fails in older versions of py-jinja2. See related bug reports [1, 2]. Since `networks.update` on its own is enough, stop assigning its result back to 'networks'. [1] pallets/jinja#641 [2] pallets/jinja#164 Change-Id: I303388223bdec9ae43e49d988aee41b5b47c1562 Signed-off-by: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
Previous implementation fails in older versions of py-jinja2. See related bug reports [1, 2]. Since `networks.update` on its own is enough, stop assigning its result back to 'networks'. [1] pallets/jinja#641 [2] pallets/jinja#164 Change-Id: I303388223bdec9ae43e49d988aee41b5b47c1562 Signed-off-by: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
template:
result:
wtf?
Please make a method of assigning a global variable.
The text was updated successfully, but these errors were encountered: