1

I am currently trying to fix an issue by using jinja variables, but somehow the variable does not keep the value outside the loop, even though I declared it before the loop begins:

{% set disablecounter = 0 %}

{% if disablecounter == 0 %}
    {% for einzelroom in all_einzelzimmer %}
        {% if zimmer.id == einzelroom.zimmer_id %}
            {% set price = einzelroom.preis %}
            <div class="preis-element">
                <p class="preis"> <span class="smallab"> ab </span> {{ price|int }}&euro; </p>
            </div>
            {% set disablecounter = disablecounter + 1 %}
            {{ disablecounter }}
        {% endif %}
    {% endfor %}
{% endif %}

{{ disablecounter }}

The variable is disablecounter inside the loop it is 1 but outside it is still 0

Thanks!

EDIT

Surrounding with a with statement also didnt worked:

{% with foo = 42 %}
    {{ foo }}
{% endwith %}

{% with %}
    {% set foo = 42 %}
    {{ foo }}
{% endwith %}
Roman
  • 3,563
  • 5
  • 48
  • 104

1 Answers1

0

I found a great solution here on SO by @Chris Warth.

Original answer by @Peter Hollingsworth: https://stackoverflow.com/a/32700975/5291566

{% with disablecounter = [0] %}

{% if disablecounter == [0] %}
    {% for einzelroom in all_einzelzimmer %}
        {% if zimmer.id == einzelroom.zimmer_id %}
            <div class="preis-element">
                <p class="preis"> <span class="smallab"> ab </span> {{ einzelroom.preis|int }}&euro; </p>
            </div>
            {% if disablecounter.append(disablecounter.pop() + 1) %}{% endif %} 
        {% endif %}
    {% endfor %}
{% endif %} 
Community
  • 1
  • 1
Roman
  • 3,563
  • 5
  • 48
  • 104