Counting the number of elements in array

MikeHolford picture MikeHolford · Jul 16, 2013 · Viewed 98.2k times · Source

I am looking to count the number of entries I have in an array in Twig. This is the code I've tried:

{%for nc in notcount%}
{{ nc|length }}
{%endfor%}

This however only produces the length of the string of one of the values in the array.

{{nc}} will produce an output of all the values of the array (there are 2) but I want the output to be just the number 2 (the count) and not all the information in the array.

Answer

Paul picture Paul · Jul 16, 2013

Just use the length filter on the whole array. It works on more than just strings:

{{ notcount|length }}