<p>I found the answer in the Twig documentation:
<a href="http://twig.sensiolabs.org/doc/templates.html#variables" rel="nofollow">http://twig.sensiolabs.org/doc/templates.html#variables</a></p>

<blockquote>
  <p>You can use a dot (.) to access attributes of a variable (methods or properties of a PHP object, or items of a PHP array), or the so-called "subscript" syntax ([]):</p>
</blockquote>

<pre><code>{{ foo.bar }}
{{ foo['bar'] }}
</code></pre>

<p>When using the subscript syntax, you can concatenate the loop.index like so:</p>

<pre><code>{% for i is 1..5 %}

    {{ entry['item' ~ loop.index] }}

{% endfor %}
</code></pre>

<p>Or even simpler, use the <code>i</code> variable from the for loop.</p>

<pre><code>{% for i is 1..5 %}

    {{ entry['item' ~ i] }}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-12-17T13:07:38.583Z
2015-04-13T09:31:00.420Z
2015-04-13T09:31:00.420Z
Alex Roper
<p>Let's say I have 5 Craft fields named "item1" thru "item5."</p>

<p>Instead of writing out each variable...</p>

<pre><code>{{ entry.item1 }}
{{ entry.item2 }}
{{ entry.item3 }}
{{ entry.item4 }}
{{ entry.item5 }}
</code></pre>

<p>How can I create a loop to do this for me?</p>

<p>I've tried this...</p>

<pre><code>{% for i is 1..5 %}

    {{ entry.item ~ loop.index }}

{% endfor %}
</code></pre>

<p>But it returns the error:</p>

<blockquote>
  <p>Craft\EntryModel and its behaviors do not have a method or closure named "item".</p>
</blockquote>

<templating><variable><forloop><loop>
Use loop index number in a Twig variable
# Use loop index number in a Twig variable

(Asked by: username_0 on 2014-12-17)

Let's say I have 5 Craft fields named "item1" thru "item5."
Instead of writing out each variable...
{{ entry.item1 }}
{{ entry.item2 }}
{{ entry.item3 }}
{{ entry.item4 }}
{{ entry.item5 }}

How can I create a loop to do this for me?
I've tried this...
{% for i is 1..5 %}

    {{ entry.item ~ loop.index }}

{% endfor %}

But it returns the error:

Craft\EntryModel and its behaviors do not have a method or closure named "item".

**Tags:** templating, variable, forloop, loop


---

## 1 answers

### Answer by username_0 on 2014-12-17. Score: 8 | Accepted answer

I found the answer in the Twig documentation:
http://twig.sensiolabs.org/doc/templates.html#variables

You can use a dot (.) to access attributes of a variable (methods or properties of a PHP object, or items of a PHP array), or the so-called "subscript" syntax ([]):

{{ foo.bar }}
{{ foo['bar'] }}

When using the subscript syntax, you can concatenate the loop.index like so:
{% for i is 1..5 %}

    {{ entry['item' ~ loop.index] }}

{% endfor %}

Or even simpler, use the i variable from the for loop.
{% for i is 1..5 %}

    {{ entry['item' ~ i] }}

{% endfor %}


---

