📝 Edit page
➕ Add page
For loops
These are not filters, as they cannot be used alone. Only in a for
loop.
Given array:
my_array:
- 1
- 2
- 3
For loop modifiers
Limit
Get the first N items.
{% for item in page.my_array limit: 2 %}
{{ item }}
{% endfor %}
Skip the first items and start at the Nth item.
{% for item in page.my_array offset: 2 %}
{{ item }}
{% endfor %}
Combine offset and limit e.g. start at 2nd item and get 2 items.
{% for item in page.my_array offset: 2 limit: 2 %}
{{ item }}
{% endfor %}
See also All Filters page for slice
filter.
Reversed
{% for item in page.my_array reversed %}
{{ item }}
{% endfor %}
For loop variables
Index
Index of the current iteration.
Use .index
to start at one or .index0
to start at zero. And first and last for whether at the start or end of the loop.
forloop.index
forloop.index0
forloop.first
forloop.last
e.g.
{% for item in page.my_array %}
{{ forloop.index }} <!-- e.g. 1, 2, 3 -->
{{ forloop.index0 }} <!-- e.g. 0, 1, 2 -->
{{ forloop.last }} <!-- e.g. false, false, true -->
{% endfor %}
Using last
is useful if you are rendering a separator. Like a comma between JSON values or a pipe symbol |
beween items in a menu.
{
{% for key_value in page.my_array %}
{{ key_value[0] | jsonify }}: {{ key_value[1] | jsonify }}
{% unless forloop.last %},{% endunless %}
{% endfor %}
}
To get:
{
"abc": "123",
"def": "xyz"
}
Length
{{ forloop.length }}