batch

The batch filter "batches" items by returning a list of lists with the given number of items. A second parameter can be provided and used to fill in missing items:

バッチ フィルタは、指定された数のアイテムを含むリストのリストを返すことによって、アイテムを「バッチ処理」します。 2 番目のパラメーターを指定して、不足している項目を埋めるために使用できます。
1
2
3
4
5
6
7
8
9
10
11
{% set items = ['a', 'b', 'c', 'd'] %}

<table>
    {% for row in items|batch(3, 'No item') %}
        <tr>
            {% for column in row %}
                <td>{{ column }}</td>
            {% endfor %}
        </tr>
    {% endfor %}
</table>

The above example will be rendered as:

上記の例は次のようにレンダリングされます。
1
2
3
4
5
6
7
8
9
10
11
12
<table>
    <tr>
        <td>a</td>
        <td>b</td>
        <td>c</td>
    </tr>
    <tr>
        <td>d</td>
        <td>No item</td>
        <td>No item</td>
    </tr>
</table>

Arguments

  • size: The size of the batch; fractional numbers will be rounded up
    size: バッチのサイズ。小数は切り上げます
  • fill: Used to fill in missing items
    fill: 不足している項目を埋めるために使用されます
  • preserve_keys: Whether to preserve keys or not
    preserve_keys: キーを保持するかどうか