Get a Demo

for

The for tag loops over items in a sequence (array or hash).

For example, to loop over a list of blog posts you could do the following:

{% for post in posts %}
    <h2>{{ post.postTitle }}</h2>
{% endfor %}

The "post" value will hold each value in the posts array as it's looped through.

The loop variable Link

Inside of a for loop block you have access to some special variables. These variables are not accessible outside of the for loop.

The loop variable holds an array of values.

Iterate over a sequence of numbers or letters. Link

If you need to iterate over a sequence of numbers you can use the .. operator.

{% for i in 0..10 %}
    {{ i }}<br>
{% endfor %}

The above will output all numbers from 0 to 10 with each on a new line.

You can do a similar thing with letters.

{% for letter in 'a'...'z' %}
    {{ letter }}<br>
{% endfor %}
Variable Description
loop.index

The current iteration of the loop starting at 1.

{{ loop.index }}

loop.index0

The current iteration of the loop starting at 0.

{{ loop.index0 }}

loop.revindex

The number of iterations from the end of the loop starting at 1.

{{ loop.revindex }}

loop.revindex0

The number of iterations from the end of the loop starting at 0.

{{ loop.revindex0 }}

loop.first

Holds whether or not the current iteration is the first iteration. True if it's the first iteration, false if it's not.

{% if loop.first %}
{% endif %}

loop.last

Holds whether or not the current iteration is the last iteration. True if it's the last iteration, false if it's not.

{% if loop.last %}
{% endif %}

loop.length

Holds the number of items in the loop sequence.

{{ loop.length }}

loop.parent

Holds the parent context; the variable being looped through.

{% for post in posts %}
    {{ loop.parent|debug('parent') }}
{% endfor %}

In the above code loop.parent would hold the posts value.

The loop.length, loop.revindex, loop.revindex0 and loop.last variables are not available when looping with a condition.

Adding a loop condition Link

You cannot force breaking out of a loop, however, you can add a condition that allows you to skip items in the loop. 

The following would skip all users which are not active.

<ul>
    {% for user in users if user.active %}
        <li>{{ user.username }}</li>
    {% endfor %}
</ul>

Note, do not use the loop variable within the condition. For example, adding a condition like the following won't work as the index is only incremented when the condition is true. In this case the condition will never match.

{% for item in items if loop.index > 4 %}
{% endfor %}

The else clause Link

If no looping will occur because the sequence is empty, then you can output a replacement by using else.

{% for post in posts %}
    <p><a href="{{ post.url }}>{{ post.postTitle }}</a></p>
{% else %}
    <p>There are no posts.</p>
{% endfor %}

Iterating over keys Link

By default, the for loop will only iterate over the values of the sequence. You can use the keys filter to iterate over only the array keys.

{% for key in array|keys %}
   ...
{% endfor %}

Iterating over both keys and values Link

Below is how you would iterate over both the keys and values of an array.

{% for key, user in users %}
    <p>{{ key }}: {{ user.name }}
{% endfor %}

Iterate over a subset Link

If you wanted to iterate over a portion of the array then you can achieve this with the slice filter.

Get Started With Aptuitiv