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.
Every for
loop has an associated forloop
object with information about the loop.
Syntax
The current item in the array.
The array to iterate over.
The expression to render for each iteration.
{% for product in collection.products -%}
{{ product.title }}
{%- endfor %}
{
"collection": {
"products": [
{
"title": "Draught of Immortality"
},
{
"title": "Glacier ice"
},
{
"title": "Health potion"
},
{
"title": "Invisibility potion"
}
]
}
}
Output
for tag parameters
limit
Syntax
You can limit the number of iterations using the limit
parameter.
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 %}
{
"collection": {
"products": [
{
"title": "Draught of Immortality"
},
{
"title": "Glacier ice"
},
{
"title": "Health potion"
},
{
"title": "Invisibility potion"
}
]
}
}
Output
offset
Syntax
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 %}
{
"collection": {
"products": [
{
"title": "Draught of Immortality"
},
{
"title": "Glacier ice"
},
{
"title": "Health potion"
},
{
"title": "Invisibility potion"
}
]
}
}
Output
range
Syntax
Instead of iterating over specific items in an array, you can specify a numeric range to iterate over.
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
reversed
Syntax
You can iterate in reverse order using the reversed
parameter.
{% for product in collection.products reversed -%}
{{ product.title }}
{%- endfor %}
{
"collection": {
"products": [
{
"title": "Draught of Immortality"
},
{
"title": "Glacier ice"
},
{
"title": "Health potion"
},
{
"title": "Invisibility potion"
}
]
}
}
Output
forloopobject
Information about a parent for
loop.
Properties
The parent
forloop
object.If the current
for
loop isn't nested inside anotherfor
loop, thennil
is returned.ExampleUse theparentloop
property{% for i in (1..3) -%}{% for j in (1..3) -%}{{ forloop.parentloop.index }} - {{ forloop.index }}{%- endfor %}{%- endfor %}{% for i in (1..3) -%} {% for j in (1..3) -%} {{ forloop.parentloop.index }} - {{ forloop.index }} {%- endfor %} {%- endfor %}
Output
1 - 11 - 21 - 32 - 12 - 22 - 33 - 13 - 23 - 3
{
"first": true,
"index": 1,
"index0": 0,
"last": false,
"length": 4,
"rindex": 3
}
Use the forloop
object
forloop
object{% for page in pages -%}
{%- if forloop.length > 0 -%}
{{ page.title }}{% unless forloop.last %}, {% endunless -%}
{%- endif -%}
{% endfor %}