forloop

{{ forloop }}

The forloop object contains attributes of its parent for loop (it can only be used within {% for %} and {% map %} tags).

Properties

Field Type Description
name string The name of the current forloop (automatically generated from the for tag's variable and collection names).
length number The number of items in the forloop
index number The 1-based index of the current item in the for loop.
index0 number The 0-based index of the current item in the for loop.
rindex number The 1-based index of the current item in the for loop counting from the end to the beginning (the reverse of index).
rindex0 number The 0-based index of the current item in the for loop counting from the end to the beginning (the reverse of index0).
first number True if the current item is the first item in the for loop.
last number True if the current item is the last item in the for loop.

Examples

Looping collection and accessing forloop properties

Copy
{% for item in collection %}
Item {{forloop.index}} out of {{forloop.length}}
{{ item }}
{{forloop.rindex0}} items remaining
{% endfor %}