在液体模板中迭代数组

时间:2014-03-20 16:23:33

标签: ruby jekyll liquid

我知道我可以使用以下代码迭代液体模板中的数组:

{% for item in myarray %}
    <p>{{ item.label }}</p>

但是如何在数组中获取项目的索引?

1 个答案:

答案 0 :(得分:7)

根据关于液体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)