【发布时间】:2014-03-20 16:23:33
【问题描述】:
我知道我可以使用以下代码遍历液体模板中的数组:
{% for item in myarray %}
<p>{{ item.label }}</p>
但是我怎样才能得到我的项目在数组中的索引呢?
【问题讨论】:
我知道我可以使用以下代码遍历液体模板中的数组:
{% for item in myarray %}
<p>{{ item.label }}</p>
但是我怎样才能得到我的项目在数组中的索引呢?
【问题讨论】:
根据liquid github上的"Liquid for Designers"部分...
forloop.length # => length of the entire for loop
forloop.index # => index of the current iteration
forloop.index0 # => index of the current iteration (zero based)
forloop.rindex # => how many items are still left?
forloop.rindex0 # => how many items are still left? (zero based)
forloop.first # => is this the first iteration?
forloop.last # => is this the last iteration?
所有这些都是for 循环的辅助变量。你会发现这两个最有用...
forloop.index # => index of the current iteration
forloop.index0 # => index of the current iteration (zero based)
【讨论】: