迭代/循环

Baklib

发布于:2025-01-02

迭代(或循环)标记(iteration tag)用于重复运行一段代码。

for

重复运行一段代码。for 循环中所能够使用的属性请参考 forloop (object)

输入


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

输出

hat shirt pants

break

循环过程中若干遇到 break 标记(tag)即停止循环。

输入


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

输出

1 2 3

continue

循环过程中若遇到 continue 标记(tag)则跳出当前循环。

输入


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

输出

1 2 3   5

for (parameters)

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

range

定义循环执行的范围。可利用数字或变量来定义此执行范围。

输入


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

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

输出

3 4 5
1 2 3 4

reversed

反转循环的执行顺序。注意和 reverse 过滤器(filter)的拼写是不同的。

输入


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

输出

6 5 4 3 2 1

forloop (object)

Information about a parent for loop.

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

Use the forloop object

Input

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

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

Output

orange-strawberry-banana

forloop (properties)

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

循环一组字符串并按照它们传入的顺序将其输出。每次调用 cycle 时,传入的参数中的下一个字符串将被输出。

cycle 必须用在 for 循环中。

输入


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

输出

one
two
three
one

cycle 的使用场景包括:

  • 对表格中的奇数/偶数行输出相应的类(class)

  • 在一行中的最后一列输出一个唯一的类(class)

cycle (parameters)

cycle 能够接受一个叫做 cycle group 的参数,以便满足你在模版中需要使用多个 cycle 代码块的情况。如果没有为 cycle group 命名,那么将会假定带有相同参数的 cycle 调用属于同一个组(group)。

tablerow

生成一个 HTML 表格。必须用 <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 (parameters)

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

在执行到指定的脚标(index)之后退出 tablerow 。


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

offset

在指定的脚标(index)之后开始执行 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>

提交反馈