blob: 18a227feb396c16f44a455607c41f3896a6ff948 [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001``batch``
2=========
3
4The ``batch`` filter "batches" items by returning a list of lists with the
5given number of items. A second parameter can be provided and used to fill in
6missing items:
7
8.. code-block:: html+twig
9
10 {% set items = ['a', 'b', 'c', 'd'] %}
11
12 <table>
13 {% for row in items|batch(3, 'No item') %}
14 <tr>
15 {% for column in row %}
16 <td>{{ column }}</td>
17 {% endfor %}
18 </tr>
19 {% endfor %}
20 </table>
21
22The above example will be rendered as:
23
24.. code-block:: html+twig
25
26 <table>
27 <tr>
28 <td>a</td>
29 <td>b</td>
30 <td>c</td>
31 </tr>
32 <tr>
33 <td>d</td>
34 <td>No item</td>
35 <td>No item</td>
36 </tr>
37 </table>
38
39Arguments
40---------
41
42* ``size``: The size of the batch; fractional numbers will be rounded up
43* ``fill``: Used to fill in missing items
44* ``preserve_keys``: Whether to preserve keys or not