迭代

迭代標籤會重複執行程式碼區塊。

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 布林值
last 如果目前迭代是最後一個,則傳回 true。如果不是,則傳回 false 布林值

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 布林值
col_last 如果目前的欄是該列中的最後一欄,則傳回 true。如果不是,則傳回 false 布林值
first 如果目前迭代是第一個,則傳回 true。如果不是,則傳回 false 布林值
index 目前迭代的 1 基底索引。 number
index0 目前迭代的 0 基底索引。 number
last 如果目前迭代是最後一個,則傳回 true。如果不是,則傳回 false 布林值
length 迴圈中的迭代總次數。 number
rindex 目前迭代的 1 基底索引,依相反順序排列。 number
rindex0 目前迭代的 0 基底索引,依相反順序排列。 number
row 目前列的 1 基底索引。 number