title: Jinja2: Change the value of a variable inside a loop
body: <p>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.</p>
  
  <pre><code>{% set foo = False %}
  
  {% for item in items %}
    {% set foo = True %}
    {% if foo %} Ok(1)! {% endif %}
  {% endfor %}
  
  {% if foo %} Ok(2)! {% endif %}
  </code></pre>
  
  <p>This renders:</p>
  
  <pre><code>Ok(1)!
  </code></pre>
  
  <p>So the only (bad) solution  have found so far was this:</p>
  
  <pre><code>{% set foo = [] %}
  
  {% for item in items %}
    {% if foo.append(True) %} {% endif %}
    {% if foo %} Ok(1)! {% endif %}
  {% endfor %}
  
  {% if foo %} Ok(2)! {% endif %}
  </code></pre>
  
  <p>This renders:</p>
  
  <pre><code>Ok(1)!
  Ok(2)!
  </code></pre>
  
  <p>But, its is very ugly! Is there another more elegant solution?</p>