Liquid 中的 Iteration 循环迭代标签

循环迭代标签用于重复执行代码块。

forPermalink

重复执行一段代码块。有关 for 循环中可用属性的完整列表,请参阅 forloop object 对象

输入

{% for product in collection.products %}
  {{ product.title }}
{% endfor %}

输出

帽子 衬衫 裤子

elsePermalink

for 循环提供一个备用情况,如果循环的长度为零,则会执行备用代码。

输入

{% for product in collection.products %}
  {{ product.title }}
{% else %}
  该集合为空。
{% endfor %}

输出

该集合为空。

breakPermalink

当循环遇到 break 标签时,停止迭代。

输入

{% for i in (1..5) %}
  {% if i == 4 %}
    {% break %}
  {% else %}
    {{ i }}
  {% endif %}
{% endfor %}

输出

1 2 3

continuePermalink

当循环遇到 continue 标签时,跳过当前迭代。

输入

{% for i in (1..5) %}
  {% if i == 4 %}
    {% continue %}
  {% else %}
    {{ i }}
  {% endif %}
{% endfor %}

输出

1 2 3   5

for (parameters 参数)Permalink

limitPermalink

将循环限制为指定的迭代次数。

输入

<!-- 如果 array = [1,2,3,4,5,6] -->
{% for item in array limit:2 %}
  {{ item }}
{% endfor %}

输出

1 2

offsetPermalink

从指定的索引开始循环。

输入

<!-- 如果 array = [1,2,3,4,5,6] -->
{% for item in array offset:2 %}
  {{ item }}
{% endfor %}

输出

3 4 5 6

要从上一个使用相同迭代器的循环结束的地方开始循环,可以使用特殊的关键字 continue

输入

<!-- 如果 array = [1,2,3,4,5,6] -->
{% for item in array limit: 3 %}
  {{ item }}
{% endfor %}
{% for item in array limit: 3 offset: continue %}
  {{ item }}
{% endfor %}

输出

1 2 3
4 5 6

rangePermalink

定义一个数字范围来循环遍历。范围可以通过字面量或变量定义,也可以从变量中提取。

输入

{% for i in (3..5) %}
  {{ i }}
{% endfor %}

{% assign num = 4 %}
{% assign range = (1..num) %}
{% for i in range %}
  {{ i }}
{% endfor %}

输出

3 4 5
1 2 3 4

reversedPermalink

反转循环的顺序。注意,此标志的拼写与过滤器 reverse 不同。

输入

<!-- 如果 array = [1,2,3,4,5,6] -->
{% for item in array reversed %}
  {{ item }}
{% endfor %}

输出

6 5 4 3 2 1

forloop (object 对象)Permalink

关于父级 for 循环的信息。

{
  "first": true,
  "index": 1,
  "index0": 0,
  "last": false,
  "length": 4,
  "rindex": 3
}

使用 forloop 对象Permalink

输入

{% assign smoothie_flavors = "orange, strawberry, banana" | split: ", " %}

{% for flavor in smoothie_flavors -%}
  {%- if forloop.length > 0 -%}
    {{ flavor }}{% unless forloop.last %}-{% endunless -%}
  {%- endif -%}
{% endfor %}

输出

orange-strawberry-banana

forloop (properties 属性)Permalink

属性 描述 返回值
length 循环中的总迭代次数。 number
parentloop 父级 forloop 对象。如果当前 for 循环没有嵌套在另一个 for 循环中,则返回 nil forloop
index 当前迭代的 1-based 索引。 number
index0 当前迭代的 0-based 索引。 number
rindex 当前迭代的 1-based 索引(反向)。 number
rindex0 当前迭代的 0-based 索引(反向)。 number
first 如果当前迭代是第一次,则返回 true。否则返回 false boolean
last 如果当前迭代是最后一次,则返回 true。否则返回 false boolean

cyclePermalink

循环遍历一组字符串,并按传递参数的顺序打印它们。每次调用 cycle 时,都会打印下一个字符串参数。

cycle 必须在 for 循环块中使用。

输入

{% cycle "one", "two", "three" %}
{% cycle "one", "two", "three" %}
{% cycle "one", "two", "three" %}
{% cycle "one", "two", "three" %}

输出

one
two
three
one

cycle 的用途包括:

  • 在表格行中应用奇/偶类
  • 在每行的最后一个产品缩略图中应用唯一类

cycle (parameters 参数)Permalink

cycle 接受一个“循环组”参数,用于在一个模板中需要多个 cycle 块的情况。如果没有为循环组提供名称,则假定具有相同参数的多个调用属于同一组。

输入

{% cycle "first": "one", "two", "three" %}
{% cycle "second": "one", "two", "three" %}
{% cycle "second": "one", "two", "three" %}
{% cycle "first": "one", "two", "three" %}

输出

one
one
two
two

tablerowPermalink

生成一个 HTML 表格。必须包裹在 <table></table> HTML 标签中。有关 tablerow 循环中可用属性的完整列表,请参阅 tablerowloop 对象

输入

<table>
{% tablerow product in collection.products %}
  {{ product.title }}
{% endtablerow %}
</table>

输出

<table>
  <tr class="row1">
    <td class="col1">
      Cool Shirt
    </td>
    <td class="col2">
      Alien Poster
    </td>
    <td class="col3">
      Batman Poster
    </td>
    <td class="col4">
      Bullseye Shirt
    </td>
    <td class="col5">
      Another Classic Vinyl
    </td>
    <td class="col6">
      Awesome Jeans
    </td>
  </tr>
</table>

tablerow (parameters 参数)Permalink

colsPermalink

定义表格应有多少列。

输入

{% tablerow product in collection.products cols:2 %}
  {{ product.title }}
{% endtablerow %}

输出

<table>
  <tr class="row1">
    <td class="col1">
      Cool Shirt
    </td>
    <td class="col2">
      Alien Poster
    </td>
  </tr>
  <tr class="row2">
    <td class="col1">
      Batman Poster
    </td>
    <td class="col2">
      Bullseye Shirt
    </td>
  </tr>
  <tr class="row3">
    <td class="col1">
      Another Classic Vinyl
    </td>
    <td class="col2">
      Awesome Jeans
    </td>
  </tr>
</table>

limitPermalink

在特定索引后退出 tablerow 循环。

{% tablerow product in collection.products cols:2 limit:3 %}
  {{ product.title }}
{% endtablerow %}

offsetPermalink

从特定索引后开始 tablerow 循环。

{% tablerow product in collection.products cols:2 offset:3 %}
  {{ product.title }}
{% endtablerow %}

rangePermalink

定义一个数字范围来循环遍历。范围可以通过字面量或变量定义。

<!--变量数字示例-->

{% assign num = 4 %}
<table>
{% tablerow i in (1..num) %}
  {{ i }}
{% endtablerow %}
</table>

<!--字面量数字示例-->

<table>
{% tablerow i in (3..5) %}
  {{ i }}
{% endtablerow %}
</table>

tablerowloop (object 对象)Permalink

关于父级 tablerow 循环的信息。

{
  "col": 1,
  "col0": 0,
  "col_first": true,
  "col_last": false,
  "first": true,
  "index": 1,
  "index0": 0,
  "last": false,
  "length": 5,
  "rindex": 5,
  "rindex0": 4,
  "row": 1
}

tablerowloop (属性)Permalink

属性 描述 返回值
col 当前列的 1-based 索引。 number
col0 当前列的 0-based 索引。 number
col_first 如果当前列是行中的第一列,则返回 true。否则返回 false boolean
col_last 如果当前列是行中的最后一列,则返回 true。否则返回 false boolean
first 如果当前迭代是第一次,则返回 true。否则返回 false boolean
index 当前迭代的 1-based 索引。 number
index0 当前迭代的 0-based 索引。 number
last 如果当前迭代是最后一次,则返回 true。否则返回 false boolean
length 循环中的总迭代次数。 number
rindex 当前迭代的 1-based 索引(反向)。 number
rindex0 当前迭代的 0-based 索引(反向)。 number
row 当前行的 1-based 索引。 number