81
votes

I want to change the value of the variable declared outside the loop within a loop. But always changing, it keeps the initial value outside the loop.

{% set foo = False %}

{% for item in items %}
  {% set foo = True %}
  {% if foo %} Ok(1)! {% endif %}
{% endfor %}

{% if foo %} Ok(2)! {% endif %}

This renders:

Ok(1)!

So the only (bad) solution have found so far was this:

{% set foo = [] %}

{% for item in items %}
  {% if foo.append(True) %} {% endif %}
  {% if foo %} Ok(1)! {% endif %}
{% endfor %}

{% if foo %} Ok(2)! {% endif %}

This renders:

Ok(1)!
Ok(2)!

But, its is very ugly! Is there another more elegant solution?

3
I don't think there is any other way. Perhaps you could restructure the code so that you don't need to set the variable. - Alex Morega
+1 for the question, as it became answer for me :) - Glen Swift
@Shankar Cabus: great question. This should probably be classified under Jinja Annoyances - dreftymac
I think this is question is duplicated in: stackoverflow.com/questions/7537439/… and stackoverflow.com/questions/4870346/… (just starting, can't flag the question) You can use Pashka's approach, and add jinja2.ext.do to clean it a little bit - Gerardo Roza
I found this code to be the only way to workaround construct that I was unable to use in salt+jinja: somelist|map(format)|join - Martin

3 Answers

80
votes

Try also dictionary-based approach. It seems to be less ugly.

{% set vars = {'foo': False} %}

{% for item in items %}
  {% if vars.update({'foo': True}) %} {% endif %}
  {% if vars.foo %} Ok(1)! {% endif %}
{% endfor %}

{% if vars.foo %} Ok(2)! {% endif %}

This also renders:

Ok(1)!
Ok(2)!
51
votes

as mentioned in the documentation:

Please note that assignments in loops will be cleared at the end of the iteration and cannot outlive the loop scope.

but as of version 2.10 you can use namespaces:

    {% set ns = namespace(foo=false) %}      
    {% for item in items %}
      {% set ns.foo = True %}
      {% if ns.foo %} Ok(1)! {% endif %}
    {% endfor %}

    {% if ns.foo %} Ok(2)! {% endif %}
0
votes

You could do this to clean up the template code

{% for item in items %}
  {{ set_foo_is_true(local_vars) }}
  {% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}

And in the server code use

items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_foo_is_true(local_vars):
  local_vars['foo'] = True
  return ''
env.globals['set_foo_is_true'] = set_foo_is_true    
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)

This could be generalized to the following

{{ set_local_var(local_vars, "foo", False) }}
{% for item in items %}
  {{ set_local_var(local_vars, "foo", True) }}
  {% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}

And in the server code use

items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_local_var(local_vars, name, value):
  local_vars[name] = value
  return ''
env.globals['set_local_var'] = set_local_var
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)