Twig template: find the length of an array

It's not immediately apparent in the documentation for Twig, but finding the size of an array in a twig template is simple:

{% if my_list|length > 10 %}
    ...
{% endif %}

Or, you can extract the size into a variable:

{% set size = my_list | length %}



3 comments:

  1. Great! Thanks! You'd think they'd make this a bit more obvious.

    ReplyDelete
  2. Yeah... thank you for making this publicly available.

    ReplyDelete
  3. Or {{ array | length }} for to print the array length

    ReplyDelete