Let's say that I have a code like:
{% for x in posts %}
<p>We are in item: {{ x }}</>
{% else %}
And I want to get the current index of the for loop to run an if loop, something like(logically):
{% for x in posts %}
{% if x.index = 0 %}
<p>We are in the first item!</p>
{% else %}
<p>We are in item: {{ x }}</>
{% endif %}
{% endfor %}
How to do it inside jijna2? (I use Python with Flask).
You can use loop.index
inside the loop.
{% for x in posts %}
<p>We are in item number: {{ loop.index }}</>
{% else %}
Flask Template docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With