巴克励步
发布于:2025-01-02
for 循环中所能够使用的属性请参考 forloop (object)。
{% for product in collection.products %}
{{ product.title }}
{% endfor %}
hat shirt pants
break 标记(tag)即停止循环。
{% for i in (1..5) %}
{% if i == 4 %}
{% break %}
{% else %}
{{ i }}
{% endif %}
{% endfor %}
1 2 3
continue 标记(tag)则跳出当前循环。
{% for i in (1..5) %}
{% if i == 4 %}
{% continue %}
{% else %}
{{ i }}
{% endif %}
{% endfor %}
1 2 3 5
<!-- if array = [1,2,3,4,5,6] -->
{% for item in array limit:2 %}
{{ item }}
{% endfor %}
1 2
<!-- if array = [1,2,3,4,5,6] -->
{% for item in array offset:2 %}
{{ item }}
{% endfor %}
3 4 5 6
{% for i in (3..5) %}
{{ i }}
{% endfor %}
{% assign num = 4 %}
{% for i in (1..num) %}
{{ i }}
{% endfor %}
3 4 5
1 2 3 4
reverse 过滤器(filter)的拼写是不同的。
<!-- if array = [1,2,3,4,5,6] -->
{% for item in array reversed %}
{{ item }}
{% endfor %}
6 5 4 3 2 1
for loop.{
"first": true,
"index": 1,
"index0": 0,
"last": false,
"length": 4,
"rindex": 3
}
forloop object{% 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
Property | Description | Returns |
|---|---|---|
length | The total number of iterations in the loop. | number |
parentloop | The parent forloop object. If the current for loop isn’t nested inside another for loop, then nil is returned. | forloop |
index | The 1-based index of the current iteration. | number |
index0 | The 0-based index of the current iteration. | number |
rindex | The 1-based index of the current iteration, in reverse order. | number |
rindex0 | The 0-based index of the current iteration, in reverse order. | number |
first | Returns true if the current iteration is the first. Returns false if not. | boolean |
last | Returns true if the current iteration is the last. Returns false if not. | boolean |
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 group 的参数,以便满足你在模版中需要使用多个 cycle 代码块的情况。如果没有为 cycle group 命名,那么将会假定带有相同参数的 cycle 调用属于同一个组(group)。<table> 和 </table> 这两个 HTML 标签将其包裹起来。
<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 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>
{% tablerow product in collection.products cols:2 limit:3 %}
{{ product.title }}
{% endtablerow %}
{% tablerow product in collection.products cols:2 offset:3 %}
{{ product.title }}
{% endtablerow %}
<!--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>