---
layout: default
title: forloop
nav:
group: Liquid Variables
---
# forloop
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 %}
Output
{% highlight html %}{% raw %}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
{% endraw %}{% endhighlight %}
{% anchor_link "forloop.index0", "index0" %}
Returns the current index of the for loop, starting at 0.
Input
{% highlight html %}{% raw %}
{% for product in collections.frontpage.products %}
{{ forloop.index }}
{% endfor %}{% endraw %}{% endhighlight %}
Output
{% highlight html %}{% raw %}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{% endraw %}{% endhighlight %}
{% anchor_link "forloop.last", "last" %}
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 %}
{% anchor_link "forloop.rindex", "rindex" %}
Returns forloop.index in reverse order.
Input
{% highlight html %}{% raw %}
{% for product in collections.frontpage.products %}
{{ forloop.rindex }}
{% endfor %}{% endraw %}{% endhighlight %}
Output
{% highlight html %}{% raw %}
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
{% endraw %}{% endhighlight %}
{% anchor_link "forloop.rindex0", "rindex0" %}
Returns forloop.index0 in reverse order.
Input
{% highlight html %}{% raw %}
{% for product in collections.frontpage.products %}
{{ forloop.rindex0 }}
{% endfor %}{% endraw %}{% endhighlight %}
Output
{% highlight html %}{% raw %}
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
{% endraw %}{% endhighlight %}
{% anchor_link "forloop.length", "length" %}
Returns the number of iterations of the for loop.
Input
{% highlight html %}{% raw %}
{% for product in collections.frontpage.products %}
{% capture length %}{{ forloop.length }}{% endcapture %}
{% endfor %}
{{ length }}
{% endraw %}{% endhighlight %}
Output
{% highlight html %}{% raw %}
10
{% endraw %}{% endhighlight %}