-5
{% set foo = 200 %}

 {% for item in items %}
   {% set foo = 100 %}
   {{ foo  }}
 {% endfor %}

{{foo}}

Output 100 200

foo outside of my Loop should be 100 how can i solve this issue?

dot.Py
  • 5,007
  • 5
  • 31
  • 52

1 Answers1

0

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)!
alyei
  • 43
  • 8
  • 2
    If you're going to copy answers word for word, at least have the decency of [linking to the original](https://stackoverflow.com/questions/9486393/jinja2-change-the-value-of-a-variable-inside-a-loop) – maiorano84 Oct 11 '17 at 15:30
  • I didn't get it from stackoverflow - it was from another site. – alyei Oct 13 '17 at 16:04