Forloop

Forloop

Forloop

Properties of Forloop objects
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.

Related

string

Can be any text, from the empty string ("") to the full HTML output of the template. When used alone in a conditional, all strings evaluates as true - even if they are empty or a value such as "0" or "false".

number

Can be any number, including integers, decimals, or the value 0. Any value - including 0 - evaluates as true when used alone in a conditional.

{{ tag }}

boolean

True or False

{% 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 %}

This method creates a new liquid context for storing and manipulating variables.

Related

{% gallery %}

{% articles %}

Examples

How to use the for method

Copy

Basic: iterate through a collection

{%- for item in collection -%}
{{ item }}
{%- endfor -%}
This example will iterate through the collection and output each item.

Use the forloop variables to access loop properties

{%- for item in collection -%}
<span class="index-{{ forloop.index0 }}{% if forloop.first %} first{% elsif forloop.last %} last{% endif %}">Item {{ forloop.index }} out of {{ forloop.length }}</span>
{%- endfor -%}
You can use the automatically generated forloop variable to access loop properties like index, first, last, length, and more.

Watch out for nested forloop variables

{%- for outer in collection1 -%}
{%- for inner in collection2 -%}
Outer Item #{{ forloop.index }}
{%- endfor -%}
{%- endfor -%}
Note that you cannot access the outer forloop variable from within the inner forloop. This example will output the inner forloop index, not the outer forloop index.

Correcting access to nested forloop variables

{%- for outer in collection1 -%}
{%- var outerloop = forloop -%}{%- for inner in collection2 -%}
Outer Item #{{ outerloop.index }}
{%- endfor -%}
{%- endfor -%}
To access the outer forloop from within an inner loop you can store the outer forloop variable in a new variable with a different name and then access it using the new variable.

For loop with limit, and the continue keyword

{%- var mycollection = (1..6) -%}
First Three: {% for item in mycollection limit:3 -%}
<span class="index-{{forloop.index0}}{% if forloop.first %} first{% endif %}{% if forloop.last %} last{% endif %}">{{item}}</span>
{%- endfor %}
The Rest: {% for item in mycollection offset:continue -%}
<span class="index-{{forloop.index0}}{% if forloop.first %} first{% endif %}{% if forloop.last %} last{% endif %}">{{item}}</span>
{%- endfor -%}
First Three: <span class="index-0 first">1</span><span class="index-1">2</span><span class="index-2 last">3</span>
The Rest: <span class="index-0 first">4</span><span class="index-1">5</span><span class="index-2 last">6</span>
This example breaks a single collection into two separate loops using the limit and continue arguments. Note that the forloop variables (first, last, index, etc.) are reset for each loop.

For loop reversed

{%- for number in (1..10) reversed -%}
{%- if number == 9 -%}
{%- continue -%}
{%- elseif number == 6 -%}
{%- break -%}
{%- endif -%}
<span class="index-{{forloop.index0}} {% if forloop.first %}first {% endif %}{% if forloop.last %}last {% endif %}{% cycle exampleclasses:'even', 'odd' %}">{{number}}</span>
{%- endfor -%}
<span class="index-0 first even">10</span><span class="index-2 odd">8</span><span class="index-3 even">7</span>
This example reverses the list before looping through it. Note that the list is reversed BEFORE looping through it, so the indexes may appear to be in reverse order but they are not. Also note that the continue and break keywords work as expected and do not affect the forloop.first and forloop.last properties.

For loop with numeric offset

{%- var items = (1..8) -%}
Skip first 2: {% for item in items offset:2 -%}
{%- unless forloop.first %} {% endunless %}{{ item }}
{%- endfor -%}
Skip first 2: 3 4 5 6 7 8
Use a numeric offset to skip the first N items in the collection. Here offset:2 skips 1 and 2, so the loop outputs 3 through 8.

Demonstrates how to looping collection and accessing forloop properties.

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 -%}

Demonstrates how to loop Post Parameters.

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>

Demonstrates how to to map a collection of entities into a human-readable list of titles linking to the entities

Display linked titles from a list of items

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

The linked_title property is a shortcut to output the entity title, optionally inside a link if the entity has a url. This example takes it one step further by outputting the list according to common English usage - as a comma-separated list with the last item preceeded by "and" and handling lists with only one or two items appropriately. If there are no items in the list, then the output is "(none)".

Demonstrates how to list all keys and values for a labels field.

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>