Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Twig loops can be organized into groups using the batch filter.

The batch filter allows you to group items in a loop and specify a specific group size. For example, if you have a set of 10 items and want to group them into sets of 3, you can use the batch filter to do this.

Here's an example:

{% set items = ['Apple', 'Banana', 'Orange', 'Grape', 'Watermelon', 'Kiwi', 'Pineapple', 'Mango', 'Peach', 'Pear'] %}

{% for group in items|batch(3) %}
  <div>
    {% for item in group %}
      {{ item }}
    {% endfor %}
  </div>
{% endfor %}

In this example, we have an array of 10 items. We use the batch filter to group them into sets of 3. The outer loop iterates over each group and the inner loop iterates over each item in the group.

The output would look like this:

<div>
  Apple
  Banana
  Orange
</div>
<div>
  Grape
  Watermelon
  Kiwi
</div>
<div>
  Pineapple
  Mango
  Peach
</div>
<div>
  Pear
</div>

As you can see, the items have been grouped into sets of 3 as specified by the batch filter.