Anonymous Anonymous - 4 months ago
276 0

Example how to detect the last item in a loop in Twig.

Twig

Twig - how to detect last item in loop

{% set numberOfItems = myArray|length %}

{% for item in myArray %}

    {% if numberOfItems == loop.index %}
    
        Last item
    
{% else %}

        Not last item

    {% endif %}

{% endfor %}
Comments