{{ forloop }}

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

Properties

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

Examples

{% for item in collection %}
	<div class="item-{{forloop.index0}}{% if forloop.first %} first{% elsif forloop.last %} last{% endif %} {% cycle forloop.name: "even", "odd" %}">
		<span class="itemcount">Item {{forloop.index}} out of {{forloop.length}}</span><br />
		<span class="itemvalue">{{ item }}</span><br />
		<span class="itemsleft">{{forloop.rindex0}} items remaining</span>
	</div>
{% endfor %}

Developer Overview

Liquid Markup