Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jinja2 variable won't update in a loop based on operation performed as expected #697

Closed
sadaf2605 opened this issue Mar 28, 2017 · 1 comment

Comments

@sadaf2605
Copy link

sadaf2605 commented Mar 28, 2017

I am setting jinja2 variable outside (before) the loop, yet it is being resetted every time the loop is being iterated.
Also asked at stackoverflow: http://stackoverflow.com/questions/43061439/jinja2-variable-wont-update-in-a-loop-based-on-operation-performed-as-expected
I have this following code:

    {% set c = 0 %}
    {% for n in n_list %}

      {% if loop.index % 3 == 0 %}

      {% else %}
          {% set l = range(3, 6) | random %}
            {% set c = c+l %}
      {% endif %}

      {{ loop.index }} {{c}} {{l}}<br>

    {% endfor %}

Expected Behavior

1 3 3
2 7 4
2 7 0
2 11 4
...

Actual Behavior

1 3 3
2 4 4
3 0
4 4 4
5 5 5 # ...
6 0
7 5 5
8 5 5
9 0
10 4 4

Template Code

        {% set c = 0 %}
        {% for n in n_list %}

          {% if loop.index % 3 == 0 %}

          {% else %}
              {% set l = range(3, 6) | random %}
                {% set c = c+l %}
          {% endif %}

          {{ loop.index }} {{c}} {{l}}<br>

        {% endfor %}

Your Environment

  • Python version: 3.6.0
  • Jinja version: 2.9.5
@ThiefMaster
Copy link
Member

Duplicate of #641

@pallets pallets deleted a comment from zhangguanzhang Aug 2, 2020
@pallets pallets locked and limited conversation to collaborators Aug 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants