Problem with boolean value

I have issues with a boolean value:

{% set active = true %}
{% for i in range(3) %}
  {% if i == 1 %}
    {% set active = false %}
  {% endif %}
  {{ active }}
{% endfor %}

The output is:

  True
  False
  True

How can the value active change back to True. Maybe it is the initial value, but I need the boolean to be valid during the loop. This is probably me making a simply mind mistake.

that is because the attrib is not maintained in the for loop…try this

{% set ns=namespace(x=true) %}
{% set active = true %}
{% for i in range(3) %}
{% if i == 1 %}
{% set ns.x = false %}
{% endif %}
{% endfor %}  
{{ ns.x }}

btw…you do know that range(3) starts from 0…

Great, thank you for the quick help!
Strange behaviour but name space does the trick to keep the value as you say.
Thank and merry x-mas!

Not so strange as more: it is as it is :slight_smile: