Skip to main content

for

Renders an expression for every item in an array.

You can do a maximum of 50 iterations with a for loop. If you need to iterate over more than 50 items, then use the paginate tag to split the items over multiple pages.


Tip

Every for loop has an associated forloop object with information about the loop.


Syntax

{% for variable in array %}
expression
{% endfor %}
variable

The current item in the array.

array

The array to iterate over.

expression

The expression to render for each iteration.

{% for product in collection.products -%}
{{ product.title }}
{%- endfor %}

Output

Draught of Immortality
Glacier ice
Health potion
Invisibility potion
Anchor to for

for tag parameters

Syntax

{% for variable in array limit: number %}
expression
{% endfor %}

You can limit the number of iterations using the limit parameter.


Tip

Limit the amount of data fetched for arrays that can be paginated with the paginate tag instead of using the limit parameter. Learn more about limiting data fetching for improved server-side performance.


{% for product in collection.products limit: 2 -%}
{{ product.title }}
{%- endfor %}

Output

Draught of Immortality
Glacier ice

Syntax

{% for variable in array offset: number %}
expression
{% endfor %}

You can specify a 1-based index to start iterating at using the offset parameter.

{% for product in collection.products offset: 2 -%}
{{ product.title }}
{%- endfor %}

Output

Health potion
Invisibility potion

Syntax

{% for variable in (number..number) %}
expression
{% endfor %}

Instead of iterating over specific items in an array, you can specify a numeric range to iterate over.


Note

You can define the range using both literal and variable values.


{% for i in (1..3) -%}
{{ i }}
{%- endfor %}

{%- assign lower_limit = 2 -%}
{%- assign upper_limit = 4 -%}

{% for i in (lower_limit..upper_limit) -%}
{{ i }}
{%- endfor %}

Output

1
2
3

2
3
4

Syntax

{% for variable in array reversed %}
expression
{% endfor %}

You can iterate in reverse order using the reversed parameter.

{% for product in collection.products reversed -%}
{{ product.title }}
{%- endfor %}

Output

Invisibility potion
Health potion
Glacier ice
Draught of Immortality

forloop
object

Information about a parent for loop.

Properties

Returns true if the current iteration is the first. Returns false if not.

The 1-based index of the current iteration.

The 0-based index of the current iteration.

Returns true if the current iteration is the last. Returns false if not.

The total number of iterations in the loop.

The parent forloop object.

If the current for loop isn't nested inside another for loop, then nil is returned.

Example
Use the parentloop property
{% for i in (1..3) -%}
{% for j in (1..3) -%}
{{ forloop.parentloop.index }} - {{ forloop.index }}
{%- endfor %}
{%- endfor %}

Output

1 - 1
1 - 2
1 - 3

2 - 1
2 - 2
2 - 3

3 - 1
3 - 2
3 - 3

The 1-based index of the current iteration, in reverse order.

The 0-based index of the current iteration, in reverse order.

{
"first": true,
"index": 1,
"index0": 0,
"last": false,
"length": 4,
"rindex": 3
}
{% for page in pages -%}
{%- if forloop.length > 0 -%}
{{ page.title }}{% unless forloop.last %}, {% endunless -%}
{%- endif -%}
{% endfor %}

Output

About us, Contact, Potion dosages
Was this page helpful?