<p>Twig offers special loop variables that makes it easy to know indexing of a loop.
You can use loop.index to count your current indexing of the loop, which will starting at 1.
If You want to start your indexing from 0 so simply add 0 after loop.index like loop.index0.</p>
<p>There are other loop variable as well.</p>
<p>Like:</p>
<ul>
<li>loop.revindex</li>
<li>loop.revindex0</li>
<li>loop.first</li>
<li>loop.last</li>
<li>loop.length</li>
</ul>

CC BY-SA 4.0
2020-12-30T05:26:29.133Z
2020-12-30T05:26:29.133Z
ZealousWeb
<p>Aside from the documentation on Craft web site <a href="https://craftcms.com/docs/3.x/dev/twig-primer.html" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/dev/twig-primer.html</a>, Twig is documented in its own site: <a href="https://twig.symfony.com/doc/3.x/" rel="nofollow noreferrer">https://twig.symfony.com/doc/3.x/</a></p>
<p>ZealousWeb's answer is documented here: <a href="https://twig.symfony.com/doc/3.x/tags/for.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/3.x/tags/for.html</a></p>
<blockquote>
<p>Inside of a for loop block you can access some special variables:</p>
<ul>
<li>loop.index The current iteration of the loop. (1 indexed)|</li>
<li>loop.index0  The current iteration of the loop. (0 indexed)</li>
<li>loop.revindex   The number of iterations from the end of the loop (1 indexed)</li>
<li>loop.revindex0  The number of iterations from the end of the loop (0 indexed)</li>
<li>loop.first  True if first iteration</li>
<li>loop.last   True if last iteration</li>
<li>loop.length     The number of items in the sequence</li>
<li>loop.parent     The parent context</li>
</ul>
<pre><code>{% for user in users %}
   {{ loop.index }} - {{ user.username }}
{% endfor %}
</code></pre>
<p>The loop.length, loop.revindex, loop.revindex0, and loop.last variables are only available for PHP arrays, or objects that implement the Countable interface.</p>
</blockquote>

CC BY-SA 4.0
2020-12-31T10:45:57.360Z
2020-12-31T10:45:57.360Z
pecus
<p>Is it Possible to count current indexing in a loop which starts from indexing 1 as well as 0?</p>

<craft3>
How can I count current indexing inside a loop?
# How can I count current indexing inside a loop?

(Asked by: username_0 on 2020-12-30)

Is it Possible to count current indexing in a loop which starts from indexing 1 as well as 0?

**Tags:** craft3


---

## 2 answers

### Answer by username_1 on 2020-12-30. Score: 5 | Accepted answer

Twig offers special loop variables that makes it easy to know indexing of a loop.
You can use loop.index to count your current indexing of the loop, which will starting at 1.
If You want to start your indexing from 0 so simply add 0 after loop.index like loop.index0.

There are other loop variable as well.

Like:

* loop.revindex
* loop.revindex0
* loop.first
* loop.last
* loop.length


---

### Answer by username_2 on 2020-12-31. Score: 0

Aside from the documentation on Craft web site https://craftcms.com/docs/3.x/dev/twig-primer.html, Twig is documented in its own site: https://twig.symfony.com/doc/3.x/
username_1's answer is documented here: https://twig.symfony.com/doc/3.x/tags/for.html

Inside of a for loop block you can access some special variables:

loop.index The current iteration of the loop. (1 indexed)|
loop.index0  The current iteration of the loop. (0 indexed)
loop.revindex   The number of iterations from the end of the loop (1 indexed)
loop.revindex0  The number of iterations from the end of the loop (0 indexed)
loop.first  True if first iteration
loop.last   True if last iteration
loop.length     The number of items in the sequence
loop.parent     The parent context

{% for user in users %}
   {{ loop.index }} - {{ user.username }}
{% endfor %}

The loop.length, loop.revindex, loop.revindex0, and loop.last variables are only available for PHP arrays, or objects that implement the Countable interface.


---

