``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:
.. code-block:: html+twig
{% set items = ['a', 'b', 'c', 'd'] %}
{% for row in items|batch(3, 'No item') %}
{% for index, column in row %}
| {{ index }} - {{ column }} |
{% endfor %}
{% endfor %}
The above example will be rendered as:
.. code-block:: html+twig
| 0 - a |
1 - b |
2 - c |
| 3 - d |
4 - No item |
5 - No item |
If you choose to set the third parameter ``preserve_keys`` to ``false``, the keys will be reset in each loop.
.. code-block:: html+twig
{% set items = ['a', 'b', 'c', 'd'] %}
{% for row in items|batch(3, 'No item', false) %}
{% for index, column in row %}
| {{ index }} - {{ column }} |
{% endfor %}
{% endfor %}
The above example will be rendered as:
.. code-block:: html+twig
| 0 - a |
1 - b |
2 - c |
| 0 - d |
1 - No item |
2 - No item |
Arguments
---------
* ``size``: The size of the batch; fractional numbers will be rounded up
* ``fill``: Used to fill in missing items
* ``preserve_keys``: Whether to preserve keys or not (defaults to ``true``)