Reputation: 503
I'm trying to change the value of a variable inside a loop, this way I can do some stuff for the first iteration only, then do other stuff for all the next iterations.
{% set vars = {'foo': True} %}
{% for line in project[2].split('[newline]') %}
{% if vars.foo %}
its true!
{% else %}
its false!
{% endif %}
{% vars.update({'foo': False}) %}
{% endfor %}
Output looks like 'its true! its true! its true! its true!', so Jinja definitely doesnt get the fact that the variable has been changed.
I've also tried to swap {% vars.update({'foo': False}) %}
for {% vars.foo == False %}
but it didn't worked.
Thanks for your help
Upvotes: 0
Views: 818
Reputation: 503
I fixed my problem!
No need to use a variable, Jinja2 has something that counts the number of iteration in a loop.
So I just changed my code like this:
{% for line in project[2].split('[newline]') %}
{% if loop.index == 1 %}
its true!
{% else %}
its false!
{% endif %}
{% endfor %}
Upvotes: 4