v1.9.1
重复执行代码块的迭代标签。
基本使用
for…in
重复执行一段代码。
输入
{% for product in collection.products %} |
输出
hat shirt pants |
else
指定 for 循环的集合长度为零时执行的代码块。
输入
{% for product in collection.products %} |
输出
The collection is empty. |
break
遇到 break 标签时 for 循环停止执行。
输入
{% for i in (1..5) %} |
输出
1 2 3 |
continue
遇到 continue 标签时跳过当前这次迭代。
输入
{% for i in (1..5) %} |
输出
1 2 3 5 |
forloop
在 for 循环里有一个 forloop 变量可用,用来表示迭代的当前状态。
forloop.first, forloop.last 和 forloop.length 属性:
输入
{% for i in (1..5) %} |
输出
First |
forloop.index, forloop.index0, forloop.rindex 和 forloop.rindex0 属性:
输入
index index0 rindex rindex0 |
输出
index index0 rindex rindex0 |
参数
limit
限制循环执行的次数。
输入
<!-- if array = [1,2,3,4,5,6] --> |
输出
1 2 |
offset
从指定的下标处开始循环。
输入
<!-- if array = [1,2,3,4,5,6] --> |
输出
3 4 5 6 |
range
定义一个用于循环的数字范围。可以用字面量定义范围,也可以用变量定义范围。
输入
{% for i in (3..5) %} |
输出
3 4 5 |
reversed
反转循环的顺序。注意这个参数的拼写和过滤器 reverse 不同。
输入
<!-- if array = [1,2,3,4,5,6] --> |
输出
6 5 4 3 2 1 |