A second parameter can be provided and used to fill in missing items:

{% 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:

<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>

Arguments

ArgumentDescriptionType
sizeThe size of the batch; fractional numbers will be rounded upNumber
fillUsed to fill in missing itemsString
preserve_keysWhether to preserve keys or notBoolean

Was this helpful?

Copyright © 2024 Blutui.