Edit on GitHub
Jump to docs navigation

Twig Components / Methods / foreach

Note: You are currently reading the documentation for Bolt 5.0. Looking for the documentation for Bolt 5.2 instead?

foreach is a Twig tag to create loops with the same syntax as in PHP.

{% foreach list as sublist %}
  {% foreach sublist as key => value %}
  {% endforeach %}
{% endforeach %}

Internally it behaves the exact same way as for: it actually creates ForNode elements, so you have the same functionality like in for loops, including the loop variable and else. else works the same as with for:

{% foreach list as sublist %}
    {% foreach sublist as key => value %}
    {% else %}
        # Array "sublist" is empty / no iteration took place #
    {% endforeach %}
{% else %}
    # Array "list" is empty / no iteration took place #
{% endforeach %}

Source: Squirrelphp



Edit this page on GitHub
Couldn't find what you were looking for? We are happy to help you in the forum, on Slack or on Github.