for

Loop over each item in a sequence. For example, to display a list of users provided in a variable called users:

シーケンス内の各アイテムをループします。たとえば、users という変数で指定されたユーザーのリストを表示するには、次のようにします。
1
2
3
4
5
6
<h1>Members</h1>
<ul>
    {% for user in users %}
        <li>{{ user.username|e }}</li>
    {% endfor %}
</ul>

Note

ノート

A sequence can be either an array or an object implementing the Traversable interface.

シーケンスは、Traversable インターフェイスを実装する配列またはオブジェクトのいずれかです。

If you do need to iterate over a sequence of numbers, you can use the .. operator:

一連の数値を反復処理する必要がある場合は、.. 演算子を使用できます。
1
2
3
{% for i in 0..10 %}
    * {{ i }}
{% endfor %}

The above snippet of code would print all numbers from 0 to 10.

上記のコード スニペットは、0 から 10 までのすべての数字を出力します。

It can be also useful with letters:

文字にも役立ちます。
1
2
3
{% for letter in 'a'..'z' %}
    * {{ letter }}
{% endfor %}

The .. operator can take any expression at both sides:

.. 演算子は、両側で任意の式を取ることができます。
1
2
3
{% for letter in 'a'|upper..'z'|upper %}
    * {{ letter }}
{% endfor %}

If you need a step different from 1, you can use the range function instead.

1 以外のステップが必要な場合は、代わりに範囲関数を使用できます。

The `loop` variable

Inside of a for loop block you can access some special variables:

for ループ ブロック内では、いくつかの特別な変数にアクセスできます。
Variable Description
loop.index The current iteration of the loop. (1 indexed)
loop.index0 The current iteration of the loop. (0 indexed)
loop.revindex The number of iterations from the end of the loop (1 indexed)
loop.revindex0 The number of iterations from the end of the loop (0 indexed)
loop.first True if first iteration
loop.last True if last iteration
loop.length The number of items in the sequence
loop.parent The parent context
1
2
3
{% for user in users %}
    {{ loop.index }} - {{ user.username }}
{% endfor %}

Note

ノート

The loop.length, loop.revindex, loop.revindex0, and loop.last variables are only available for PHP arrays, or objects that implement the Countable interface.

loop.length、loop.revindex、loop.revindex0、および loop.last 変数は、PHP 配列、または Countable インターフェイスを実装するオブジェクトでのみ使用できます。

The `else` Clause

If no iteration took place because the sequence was empty, you can render a replacement block by using else:

シーケンスが空だったために反復が行われなかった場合は、else を使用して置換ブロックをレンダリングできます。
1
2
3
4
5
6
7
<ul>
    {% for user in users %}
        <li>{{ user.username|e }}</li>
    {% else %}
        <li><em>no user found</em></li>
    {% endfor %}
</ul>

Iterating over Keys

By default, a loop iterates over the values of the sequence. You can iterate on keys by using the keys filter:

デフォルトでは、ループはシーケンスの値を繰り返します。 keys フィルターを使用して、キーを反復処理できます。
1
2
3
4
5
6
<h1>Members</h1>
<ul>
    {% for key in users|keys %}
        <li>{{ key }}</li>
    {% endfor %}
</ul>

Iterating over Keys and Values

You can also access both keys and values:

キーと値の両方にアクセスすることもできます。
1
2
3
4
5
6
<h1>Members</h1>
<ul>
    {% for key, user in users %}
        <li>{{ key }}: {{ user.username|e }}</li>
    {% endfor %}
</ul>

Iterating over a Subset

You might want to iterate over a subset of values. This can be achieved using the slice filter:

値のサブセットを反復処理したい場合があります。これは、スライス フィルターを使用して実現できます。
1
2
3
4
5
6
<h1>Top Ten Members</h1>
<ul>
    {% for user in users|slice(0, 10) %}
        <li>{{ user.username|e }}</li>
    {% endfor %}
</ul>