Counting the number of elements in array

前端 未结 3 650
你的背包
你的背包 2021-02-03 16:56

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%}
         


        
3条回答
  •  旧巷少年郎
    2021-02-03 17:20

    This expands on the answer by Denis Bubnov.

    I used this to find child values of array elements—namely if there was a anchor field in paragraphs on a Drupal 8 site to build a table of contents.

    {% set count = 0 %}
    {% for anchor in items %}
        {% if anchor.content['#paragraph'].field_anchor_link.0.value %}
            {% set count = count + 1 %}
        {% endif %}
    {% endfor %}
    
    {% if count > 0 %}
     ---  build the toc here --
    {% endif %}
    

提交回复
热议问题