迭代
迭代标签重复运行代码块。
for
重复执行代码块。有关 for
循环中可用的属性的完整列表,请参阅 forloop
对象。
输入
{% for product in collection.products %}
{{ product.title }}
{% endfor %}
输出
hat shirt pants
else
为 for
循环指定回退情况,如果循环长度为零,则将运行该回退情况。
输入
{% for product in collection.products %}
{{ product.title }}
{% else %}
The collection is empty.
{% endfor %}
输出
The collection is empty.
break
当遇到 break
标签时,导致循环停止迭代。
输入
{% for i in (1..5) %}
{% if i == 4 %}
{% break %}
{% else %}
{{ i }}
{% endif %}
{% endfor %}
输出
1 2 3
continue
当遇到 continue
标签时,导致循环跳过当前迭代。
输入
{% for i in (1..5) %}
{% if i == 4 %}
{% continue %}
{% else %}
{{ i }}
{% endif %}
{% endfor %}
输出
1 2 3 5
for (参数)
limit
将循环限制为指定的迭代次数。
输入
<!-- if array = [1,2,3,4,5,6] -->
{% for item in array limit:2 %}
{{ item }}
{% endfor %}
输出
1 2
offset
从指定的索引开始循环。
输入
<!-- if array = [1,2,3,4,5,6] -->
{% for item in array offset:2 %}
{{ item }}
{% endfor %}
输出
3 4 5 6
要从上次使用相同迭代器的循环停止的位置开始循环,请传递特殊单词 continue
。
输入
<!-- if 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
range
定义要循环遍历的数字范围。该范围可以由字面量和变量数字定义,并且可以从变量中提取。
输入
{% 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
reversed
反转循环的顺序。请注意,此标志的拼写与过滤器 reverse
不同。
输入
<!-- if array = [1,2,3,4,5,6] -->
{% for item in array reversed %}
{{ item }}
{% endfor %}
输出
6 5 4 3 2 1
forloop (对象)
有关父 for
循环的信息。
{
"first": true,
"index": 1,
"index0": 0,
"last": false,
"length": 4,
"rindex": 3
}
使用 forloop
对象
输入
{% 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 (属性)
属性 | 描述 | 返回 |
---|---|---|
length |
循环中的总迭代次数。 | number |
parentloop |
父 forloop 对象。如果当前的 for 循环没有嵌套在另一个 for 循环中,则返回 nil 。 |
forloop |
index |
当前迭代的从 1 开始的索引。 | number |
index0 |
当前迭代的从 0 开始的索引。 | number |
rindex |
当前迭代的从 1 开始的索引,反向顺序。 | number |
rindex0 |
当前迭代的从 0 开始的索引,反向顺序。 | number |
first |
如果当前迭代是第一个,则返回 true 。如果不是,则返回 false 。 |
boolean |
last |
如果当前迭代是最后一个,则返回 true 。如果不是,则返回 false 。 |
boolean |
cycle
循环遍历一组字符串,并按照它们作为参数传递的顺序打印它们。每次调用 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 (参数)
在需要在一个模板中使用多个 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
tablerow
生成 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 (参数)
cols
定义表格应具有多少列。
输入
{% 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>
limit
在特定索引后退出 tablerow
循环。
{% tablerow product in collection.products cols:2 limit:3 %}
{{ product.title }}
{% endtablerow %}
offset
在特定索引后开始 tablerow
循环。
{% tablerow product in collection.products cols:2 offset:3 %}
{{ product.title }}
{% endtablerow %}
range
定义要循环遍历的数字范围。该范围可以由字面量和变量数字定义。
<!--variable number example-->
{% assign num = 4 %}
<table>
{% tablerow i in (1..num) %}
{{ i }}
{% endtablerow %}
</table>
<!--literal number example-->
<table>
{% tablerow i in (3..5) %}
{{ i }}
{% endtablerow %}
</table>
tablerowloop (对象)
有关父 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 (属性)
属性 | 描述 | 返回 |
---|---|---|
col |
当前列的从 1 开始的索引。 | number |
col0 |
当前列的从 0 开始的索引。 | number |
col_first |
如果当前列是行中的第一列,则返回 true 。如果不是,则返回 false 。 |
boolean |
col_last |
如果当前列是行中的最后一列,则返回 true 。如果不是,则返回 false 。 |
boolean |
first |
如果当前迭代是第一个,则返回 true 。如果不是,则返回 false 。 |
boolean |
index |
当前迭代的从 1 开始的索引。 | number |
index0 |
当前迭代的从 0 开始的索引。 | number |
last |
如果当前迭代是最后一个,则返回 true 。如果不是,则返回 false 。 |
boolean |
length |
循环中的总迭代次数。 | number |
rindex |
当前迭代的从 1 开始的索引,反向顺序。 | number |
rindex0 |
当前迭代的从 0 开始的索引,反向顺序。 | number |
row |
当前行的从 1 开始的索引。 | number |