跳到主要内容

forloop

父级 for loop 的信息。

PropertiesTypeDescription
firstboolean若当前迭代为第一次,则返回 true;否则返回 false
lastboolean若当前迭代为最后一次,则返回 true;否则返回 false
indexnumber当前迭代的从 1 开始的索引。
index0number当前迭代的从 0 开始的索引。
lengthnumber循环中的迭代总次数。
rindexnumber当前迭代的从 1 开始的逆序索引。
rindex0number当前迭代的从 0 开始的逆序索引。
{
"first":true,
"last":false,
"index":1,
"index0":0,
"rindex":4,
"rindex0":3,
"length":4
}

使用 forloop object

{% for i in (1..5) %}
{% if forloop.length > 0 %}
{{ i }}{% unless forloop.last %},{% endunless %}
{% endif %}
{% endfor %}
1, 2, 3, 4, 5