【问题标题】:Iterate over arrays in liquid templates迭代液体模板中的数组
【发布时间】:2014-03-20 16:23:33
【问题描述】:

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

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

但是我怎样才能得到我的项目在数组中的索引呢?

【问题讨论】:

    标签: ruby jekyll liquid


    【解决方案1】:

    根据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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多