How to output loop.counter in python jinja template?
Asked Answered
A

4

288

I want to be able to output the current loop iteration to my template.

According to the docs, there is a loop.counter variable that I am trying to use:

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

But is being outputed to my template. What is the correct syntax?

Ardoin answered 27/8, 2012 at 16:2 Comment(0)
S
585

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

Swabber answered 27/8, 2012 at 18:8 Comment(2)
What if there is nested-for loop? How will it work there? How to access outer for loop's index when inside of the nested for?Ardenardency
It is possible to store the outer loop index in a variable. See: https://mcmap.net/q/109824/-get-loop-index-of-outer-loopIdeomotor
U
126

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).
Uturn answered 25/7, 2014 at 15:24 Comment(0)
A
35

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>
Ascot answered 10/1, 2019 at 2:52 Comment(3)
I am getting jinja2.exceptions.UndefinedError: 'forloop' is undefinedTorment
use loop with jinja; here is the documentation. forloop is for django templates.Ascot
The question is clearly asking about jinja, why are we giving answers about Django templates here...Infinitude
B
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 %}
Brahui answered 6/8, 2021 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.