The forloop object contains attributes of its parent for loop.
{% block "note-information" %}
The forloop object can only be used within for tags.
{% endblock %}
{% table_of_contents %}
{% anchor_link "forloop.first", "first" %}
Returns true if it's the first iteration of the for loop. Returns false if it is not the first iteration.
Input
{% highlight html %}{% raw %}
{% for product in collections.frontpage.products %}
{% if forloop.first == true %}
First time through!
{% else %}
Not the first time.
{% endif %}
{% endfor %}
{% endraw %}{% endhighlight %}
Output
{% highlight html %}{% raw %}
First time through!
Not the first time.
Not the first time.
Not the first time.
Not the first time.
{% endraw %}{% endhighlight %}
{% anchor_link "forloop.index", "index" %}
Returns the current index of the for loop, starting at 1.
Input
{% highlight html %}{% raw %}
{% for product in collections.frontpage.products %}
{{ forloop.index }}
{% endfor %}{% endraw %}{% endhighlight %}
Returns true if it's the last iteration of the for loop. Returns false if it is not the last iteration.
Input
{% highlight html %}{% raw %}
{% for product in collections.frontpage.products %}
{% if forloop.last == true %}
This is the last iteration!
{% else %}
Keep going...
{% endif %}
{% endfor %}
{% endraw %}{% endhighlight %}
Output
{% highlight html %}{% raw %}
Keep going...
Keep going...
Keep going...
Keep going...
Keep going...
This is the last iteration!
{% endraw %}{% endhighlight %}