Twig: loop.last does not work with for ... if syntax

I have a problem with this file : https://github.com/KnpLabs/KnpDisqusBundle/blob/master/Resources/views/js.html.twig , which I reproduced more simply, like this:

{% for test in 0..3 if true %}
   {{loop.last}}
{% endfor %}

This raises the following error :

Item “last” for “Array” does not exist

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 17 (10 by maintainers)

Commits related to this issue

Most upvoted comments

For Drupal, just use the Twig Tweak module’s “children” filter, like so:

{% for item in content.field_name | children(true) %}
  {# loop.length, loop.revindex, loop.revindex0, and loop.last are now available #}
{% endfor %}

End of the issue for me, thank you so much for helping me solving it.