a Sensio Labs Product

The flexible, fast, and secure
template engine for PHP

Filters » batch

Questions & Feedback

License

Twig documentation is licensed under the new BSD license.

batchΒΆ

New in version 1.12.3: The batch filter was added in Twig 1.12.3.

The batch filter "batches" items by returning a list of lists with the given number of items. If you provide a second parameter, it is used to fill missing items:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] %}

<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
13
14
15
16
17
<table>
    <tr>
        <td>a</td>
        <td>b</td>
        <td>c</td>
    </tr>
    <tr>
        <td>d</td>
        <td>e</td>
        <td>f</td>
    </tr>
    <tr>
        <td>g</td>
        <td>No item</td>
        <td>No item</td>
    </tr>
</table>
This website is powered by PHP and Twig. The Twig logo is © 2010-2012 Sensio Labs