The Python Oracle

How to output loop.counter in python jinja template?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Techno Bleepage Open

--

Chapters
00:00 Question
00:32 Accepted answer (Score 541)
01:15 Answer 2 (Score 96)
01:45 Answer 3 (Score 29)
02:04 Answer 4 (Score 0)
02:25 Thank you

--

Full question
https://stackoverflow.com/questions/1214...

Question links:
[the docs]: https://jinja.palletsprojects.com/en/3.0.../

Accepted answer links:
http://jinja.pocoo.org/docs/templates/

Answer 2 links:
[the official docs]: https://jinja.palletsprojects.com/en/3.0...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #jinja2 #templatingengine

#avk47



ACCEPTED ANSWER

Score 592


The counter variable inside the loop is called loop.index in Jinja2.

>>> from jinja2 import Template

>>> s = "{% for element in elements %}{{loop.index}} {% endfor %}"
>>> Template(s).render(elements=["a", "b", "c", "d"])
1 2 3 4

In addition to loop.index, there is also




ANSWER 2

Score 130


Inside of a for-loop block, you can access some special variables, such as loop.index (but not loop.counter). From the official docs:

Variable Description
loop.index The current iteration of the loop. (1 indexed)
loop.index0 The current iteration of the loop. (0 indexed)
loop.revindex The number of iterations from the end of the loop (1 indexed)
loop.revindex0 The number of iterations from the end of the loop (0 indexed)
loop.first True if first iteration.
loop.last True if last iteration.
loop.length The number of items in the sequence.
loop.cycle A helper function to cycle between a list of sequences.
loop.depth Indicates how deep in a recursive loop the rendering currently is. Starts at level 1
loop.depth0 Indicates how deep in a recursive loop the rendering currently is. Starts at level 0
loop.previtem The item from the previous iteration of the loop. Undefined during the first iteration.
loop.nextitem The item from the following iteration of the loop. Undefined during the last iteration.
loop.changed(*val) True if previously called with a different value (or not called at all).



ANSWER 3

Score 36


If you are using Django, use forloop.counter instead of loop.counter:

<ul>
{% for user in userlist %}
  <li>
      {{ user }} {{forloop.counter}}
  </li>
      {% if forloop.counter == 1 %}
          This is the First user
      {% endif %}
{% endfor %}
</ul>



ANSWER 4

Score 0


Real-life example:

{% for image in item['images'] %}
    {% set image_id = item_id ~ '-preview-' ~ loop.index0 %}
    <div id="{{ image_id }}" class="overlay">
        <a class="cancel" href="#{{ item_id }}"></a>
        <div class="popup">
            {% set src = image if image.startswith('http') else '/static/images/store/' ~ item_id ~ '/' ~ image %}
            <a href="{{ src }}"><img class="modal-img" src="{{ src }}"/></a>
        </div>
    </div>
{% endfor %}