ForLoop

This object contains information only within a for loop. It contains the following fields:

Variable name Object Description Note
first bool Returns true if it is the first loop iteration (false otherwise)  
index int Contains the index of the current iteration, always starting at 1  
index0 int Same as index, but always starts at 0.  
last bool Similar asfirst, but only true if it is the last iteration of theforloop.  
length int Returns the number of total iterations, this will usually be the length of the processed list.  
rindex int Like index, but counting reverse (thus the last element has index 1).  
rindex0 int Like rindex, but the last index will be 0.  

Example:


{% for c in (1..4) %}
 {% if forloop.first %}
 <b>{{ c }}</b>
 {% else %}
 <i>{{ c }}</i>
 {% endif %}
{% endfor %}

Output:


<b>1</b>
<i>2</i>
<i>3</i>
<i>4</i>

Example:


{% for c in (8..12) %}
 {{ forloop.index }} - {{ c }}
{% endfor %}

Output:


1 - 8
2 - 9
3 - 10
4 - 11
5 - 12

Example:


{% for c in (8..12) %}
 {{ forloop.index0 }} - {{ c }}
{% endfor %}

Output:


0 - 8
1 - 9
2 - 10
3 - 11
4 – 12

Example:


{% for c in (0..2) %}
 {% if forloop.last %}
 LastElement
 {% else %}
 AnyElement
 {% endif %}
{% endfor %}

Output:


AnyElement
AnyElement
LastElement

Example:


{% for p in collection.products %}
 {{ forloop.length }} - {{ p.title }}
{% endfor %}

Output:


6 - Tears of Steel
6 - Sintel
6 - Route 66
6 - Star Wreck: In the Pirkinning
6 - Big Buck Bunny
6 - Elephants Dream

Example:


{% for c in (0..5) %}
 {{ forloop.rindex }}
{% endfor %}

Output:


6
5
4
3
2
1

Example:


{% for p in collection.products %}
 {{ forloop.rindex0 }} - {{ p.title }}
{% endfor %}

Output:


5 - Tears of Steel
4 - Sintel
3 - Route 66
2 - Star Wreck: In the Pirkinning
1 - Big Buck Bunny
0 - Elephants Dream