Forloop

Forloop

Forloop

Name Type Description
name String The name of the current forloop (automatically generated from the for tag's variable and collection names).
length Number The number of items in the forloop
index Number The 1-based index of the current item in the for loop.
index0 Number The 0-based index of the current item in the for loop.
rindex Number The 1-based index of the current item in the for loop counting from the end to the beginning (the reverse of index).
rindex0 Number The 0-based index of the current item in the for loop counting from the end to the beginning (the reverse of index0).
first Boolean True if the current item is the first item in the for loop.
last Boolean True if the current item is the last item in the for loop.

{% for %}

Iterates through every item in a list. Executes and outputs a block of code to the template for each item in the list.

{% for item in collection reversed? [limit:num]? [offset:value]? %}

{% for
variable_name
 
The name for the variable to assign each item in the collection to
in
collection
 
One or more values. May use the variable arguments syntax. The list of items to iterate
reversed
 
If true, the items will be iterated in reverse order. Note that this takes effect after the offset and limit are applied
limit
offset
 
The number of items to skip at the beginning of the list. Alternatively, use the keyword "continue" to resume iteration of the list from the last time the same item and collection names were used in a for loop
%}

{% else %}

{% else %}

If there are no items to iterate, the "else" block will be executed and output instead. This may be for several reasons, such as if the collection is not a list or is empty, or if the offset is greater than the size of the list.

{% continue %}

{% continue %}

Skips to the next iteration of the current loop.

{% break %}

{% break %}

Exits the current loop immediately.

{% endfor %}

Examples

For loop with limit

Copy
Template: {% assign collection = (1..6) %}
First Three: {% for item in collection limit:3 -%}
<span class="{% if forloop.first %}first{% elsif forloop.last %}last{% endif %}">{{item}}</span>
{%- endfor %}
The Rest: {% for item in collection offset:continue -%}
<span class="{% if forloop.first %}first{% elsif forloop.last %}last{% endif %}">{{item}}</span>
{%- endfor %}
Output: First Three: <span class="first">1</span><span class="">2</span><span class="last">3</span>
The Rest: <span class="first">4</span><span class="">5</span><span class="last">6</span>

For loop reversed

Copy
Template:{% for number in (1..10) reversed -%}
{%- if number == 9 %}{% continue %}{% endif -%}
{%- if number == 6 %}{% break %}{% endif -%}
<span class="{% cycle 'even', 'odd' %}">{{number}}</span>
{%- endfor %}
Output: <span class="even">10</span><span class="odd">8</span><span class="even">7</span>

Looping collection and accessing forloop properties

Copy
{% for item in collection %}
<div class="item-{{forloop.index0}}{% if forloop.first %} first{% elsif forloop.last %} last{% endif %} {% cycle forloop.name: "even", "odd" %}">
<span class="itemcount">Item {{forloop.index}} out of {{forloop.length}}</span><br />
<span class="itemvalue">{{ item }}</span><br />
<span class="itemsleft">{{forloop.rindex0}} items remaining</span>
</div>
{% endfor %}

Loop Request Headers

Copy
{% for header in request.headers %}
{{ header }}: {{ request.headers[header] }}
{% endfor %}

Looping Request Headers with Key

Copy
{% for key in request.headers.keys %}
{{ key }}: {{ request.headers[key] }}
{% endfor %}

Loop Post Parameters

Copy
<h4>Post Parameters</h4>
<dl>
{% for param in request.post_params %}
<dt>{{param}}</dt>
<dd>
{% if request.post_params.by_name[param] is_list %}
<ul>
{% for value in request.post_params.by_name[param] %}
<li>{{ value }}</li>
{% endfor %}
</ul>
{% else %}
{{ request.post_params.by_name[param] }}
{% endif %}
</dd>
{% endfor %}
</dl>

To map a collection of entities into a human-readable list of titles linking to the entities:

Copy
{% for entity in collection.items -%}
{{ entity.linked_title }}{% unless forloop.first -%}{% if forloop.length == 2 %} and {% else %}, {% if forloop.last %}and {% endif %}{% endif %}{% endunless %}
{%- endfor %}

List all custom properties for the current user

Copy
<h4>User Properties:</h4>
<ul>
{% for property in user %} --equivalent to {% for property in user.properties %}
<li><strong>{{property}}</strong> = {{ user[property] }}</li>
{% endfor %}
</ul>

List all keys and values for a labels field

Copy
<ul>
{% for kvp in entity.labels %}
<li><strong>{{kvp.Key}}</strong> = {{kvp.Value}}</li>
{% endfor %}
</ul>