<p>If you're in the context of a Twig loop, you have access to the <a href="https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable" rel="nofollow noreferrer"><code>loop</code> variable</a>, which has a number of “magic” properties set.</p>

<p>This allows you to do something like:</p>

<pre><code>{% set posts = craft.entries({ section: 'posts' }).all %}
{% for post in posts %}
  Post #{{ loop.index }} is named: {{ post.title }}&lt;br&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-06-21T01:40:59.117Z
2018-06-21T01:40:59.117Z
August Miller
<p>I need to assign a # 1-N for each entry that is returned -- i.e., 1, 2, 3, 4, ....   (This is for a sort plugin.)</p>

<p>How do I template for that?</p>

<templating>
Assign a number 1-N for each entry that is returned
# Assign a number 1-N for each entry that is returned

(Asked by: username_0 on 2018-06-21)

I need to assign a # 1-N for each entry that is returned -- i.e., 1, 2, 3, 4, .... (This is for a sort plugin.)

How do I template for that?

**Tags:** templating

---

## 1 answers

### Answer by username_1 on 2018-06-21. Score: 1 | Accepted answer

If you're in the context of a Twig loop, you have access to the loop variable, which has a number of “magic” properties set.
This allows you to do something like:
{% set posts = craft.entries({ section: 'posts' }).all %}
{% for post in posts %}
  Post #{{ loop.index }} is named: {{ post.title }}
{% endfor %}


---

