繰り返し処理
繰り返しタグは、コードブロックを繰り返し実行します。
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
は、1つのテンプレートに複数のcycle
ブロックが必要な場合に、「サイクルグループ」パラメータを受け入れます。サイクルグループに名前が指定されていない場合、同じパラメータを持つ複数の呼び出しは1つのグループとみなされます。
入力
{% 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>
で囲む必要があります。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 |