for
Execute the encapsulated block n
times the amount of items equals to the length of the iterable array.
INFO
Every for
loop provides a local variable forloop
which contains information about the loop in progress;
{% for item in array %}
block_to_output
{% endfor %}
2
3
item
: The item of the current loop iteration.array
: Array to loop over.block_to_output
: Rendered expression in every loop iteration.
Example:
Data
{
"products": [
{
"slug": "product-1",
"price": 76
},
{
"slug": "YouCan",
"price": 100
},
{
"slug": "product-2",
"price": 30
},
{
"slug": "product-3",
"price": 124
},
]
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Note: Data is only refereed as a reference for this example and is in no means reflecting real world data.
{% for product in products %}
{{- product.slug -}} | {{- product.price -}}
{% endfor %}
2
3
Output
product-1 | 76
YouCan | 100
product-2 | 30
product-3 | 124
2
3
4
Paramaters
INFO
It's worth mentioning that these parameters can coexists with each other.
limit
Instead of going through the entire array, we can use limit
to set maximum iterations count.
{% for item in array limit: number %}
block_to_output
{% endfor %}
2
3
limit
: The number of iterations before stopping the loop.
Example:
Data
{
"products": [
{
"slug": "product-1",
"price": 76
},
{
"slug": "YouCan",
"price": 100
},
{
"slug": "product-2",
"price": 30
},
{
"slug": "product-3",
"price": 124
},
]
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Note: Data is only refereed as a reference for this example and is in no means reflecting real world data.
{% for product in products limit: 2 %}
{{- product.slug -}} | {{- product.price -}}
{% endfor %}
2
3
Output
product-1 | 76
YouCan | 100
2
offset
When specified, the loop won't start from the first available item in the array, instead, will begin at the specified position.
{% for item in array offset: number %}
block_to_output
{% endfor %}
2
3
offset
: Index to start the loop from, should be within[0, array.length - 1]
.
Example:
Data
{
"products": [
{
"slug": "product-1",
"price": 76
},
{
"slug": "YouCan",
"price": 100
},
{
"slug": "product-2",
"price": 30
},
{
"slug": "product-3",
"price": 124
},
]
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Note: Data is only refereed as a reference for this example and is in no means reflecting real world data.
{% for product in products offset: 1 %}
{{- product.slug -}} | {{- product.price -}}
{% endfor %}
2
3
Output
YouCan | 100
product-2 | 30
product-3 | 124
2
3
range
Instead of going through an array, you can instead specific two ends of a range to loop over.
{% for item in (number..number) %}
block_to_output
{% endfor %}
2
3
number..number
: the first number and the last number indicates the min and max of space we are creating to which we'll be iterating over. These values can either be hard coded or be some var reference, they both result in the same output as you'd expect.
Example:
Data
{
"max_count": 4
}
2
3
Note: Data is only refereed as a reference for this example and is in no means reflecting real world data.
{% for value in (1..max_count) %}
{{- value -}}
{% endfor %}
2
3
Output
1
2
3
4
2
3
4
reversed
Unlike the tradition loop order, you can instead start from last to first by using reversed
{% for item in array reversed %}
block_to_output
{% endfor %}
2
3
Example:
Data
{
"products": [
{
"slug": "product-1",
"price": 76
},
{
"slug": "YouCan",
"price": 100
},
{
"slug": "product-2",
"price": 30
},
{
"slug": "product-3",
"price": 124
},
]
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Note: Data is only refereed as a reference for this example and is in no means reflecting real world data.
{% for product in products reversed %}
{{- product.slug -}} | {{- product.price -}}
{% endfor %}
2
3
Output
product-3 | 124
product-2 | 30
YouCan | 100
product-1 | 76
2
3
4
Default for empty arrays
There are some cases where you want to default to an expression when your loop has no items, else
within a for
loop does just that, where it'' render the expression within when the loop length is zero
.
{% for item in array %}
block_to_output
{% else %}
default_block
{% endfor %}
2
3
4
5
item
: The item of the current loop iteration.array
: Array to loop over.block_to_output
: Rendered expression in every loop iteration.default_block
: Expression to render if the loop has no items (length of zero).
Example:
Data
{
"products": []
}
2
3
Note: Data is only refereed as a reference for this example and is in no means reflecting real world data.
{% for product in products %}
{{- product.slug -}} | {{- product.price -}}
{% else %}
There are currently no products to display
{% endfor %}
2
3
4
5
Output
There are currently no products to display
forloop
object
length
- Type:
number
- Description: Total count of the iterations in the loop.
index
- Type:
number
- Description: Index of the current iteration starting from
1
.
index0
- Type:
number
- Description: Index of the current iteration starting from
0
in reverse order.
rindex
- Type:
number
- Description: Index of the current iteration starting from
1
in reverse order.
rindex0
- Type:
number
- Description: Index of the current iteration starting from
0
.
first
- Type:
boolean
- Description: Is
true
if the current item is the first item in this iteration, otherwise returnsfalse
.
last
- Type:
boolean
- Description: Is
true
if the current item is the last item in this iteration, otherwise returnsfalse
.
Example object:
{
"length": 8,
"index": 1,
"index0": 0,
"rindex": 4,
"rindex0": 3,
"first": true,
"last": false,
}
2
3
4
5
6
7
8
9
Usage example
{% for value in (1..4) %}
<span>{{- value -}}</span>
{% unless forloop.last %}
<span>| </span>
{% endunless %}
{% endfor %}
2
3
4
5
6
Output
1 | 2 | 3 | 4