Tuesday 23 April 2013

Find the length of an array in Twig

Problem:

How do I find the length of an array using Twig?

Solution:

Use the length filter, for instance:

{% if my_array|length < 1 %}
  {# ... do default thing ... #}
{% else %}
  {# ... do something ... #}
{% endif %}

Or if you'd like to use a variable instead ...

{% set arr_size = my_array|length %}

No comments:

Post a Comment