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.
Code
{% for product in collection.products -%}
{{ product.title }}
{%- endfor %}Data
{
"collection": {
"products": [
{
"title": "Draught of Immortality"
},
{
"title": "Glacier ice"
},
{
"title": "Health potion"
},
{
"title": "Invisibility potion"
}
]
}
}Output
Output
Draught of Immortality
Glacier ice
Health potion
Invisibility potionfor 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.
Code
{% for product in collection.products limit: 2 -%}
{{ product.title }}
{%- endfor %}Data
{
"collection": {
"products": [
{
"title": "Draught of Immortality"
},
{
"title": "Glacier ice"
},
{
"title": "Health potion"
},
{
"title": "Invisibility potion"
}
]
}
}Output
Output
Draught of Immortality
Glacier iceoffset
Syntax
You can specify a 1-based index to start iterating at using the offset parameter.
Code
{% for product in collection.products offset: 2 -%}
{{ product.title }}
{%- endfor %}Data
{
"collection": {
"products": [
{
"title": "Draught of Immortality"
},
{
"title": "Glacier ice"
},
{
"title": "Health potion"
},
{
"title": "Invisibility potion"
}
]
}
}Output
Output
Health potion
Invisibility potionrange
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.
Code
{% 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
Output
1
2
3
2
3
4reversed
Syntax
You can iterate in reverse order using the reversed parameter.
Code
{% for product in collection.products reversed -%}
{{ product.title }}
{%- endfor %}Data
{
"collection": {
"products": [
{
"title": "Draught of Immortality"
},
{
"title": "Glacier ice"
},
{
"title": "Health potion"
},
{
"title": "Invisibility potion"
}
]
}
}Output
Output
Invisibility potion
Health potion
Glacier ice
Draught of Immortalityforloopobject
Information about a parent for loop.
Properties
The parent
forloopobject.If the current
forloop isn't nested inside anotherforloop, thennilis returned.ExampleUse theparentloopproperty{% for i in (1..3) -%}{% for j in (1..3) -%}{{ forloop.parentloop.index }} - {{ forloop.index }}{%- endfor %}{%- endfor %}Code
{% 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 - 3Output
1 - 1 1 - 2 1 - 3 2 - 1 2 - 2 2 - 3 3 - 1 3 - 2 3 - 3
Example
{
"first": true,
"index": 1,
"index0": 0,
"last": false,
"length": 4,
"rindex": 3
}Use the forloop object
forloop objectCode
{% for page in pages -%}
{%- if forloop.length > 0 -%}
{{ page.title }}{% unless forloop.last %}, {% endunless -%}
{%- endif -%}
{% endfor %}Output
Output
About us, Contact, Potion dosages