Tom Tom - 2 months ago 19
Twig Question

Can Twig count the array lenght with a filter parameter

I have an Account object that contains an one-to-many relation with the newsbrief members. In Twig a want to show the quantity of members. I know that I can show the quantity with the following code:

{{ account.mailList|length }}


Only the mailList contains also inactive members. With these members their field active is false. Is there a way to filter them out? Is this way of working slowing down the application?

Answer

You can use for loop in twig:

{% set activeMailCount = 0 %}
{% for mail in account.mailList if account.mailList.active %}
    {% set activeMailCount = activeMailCount + 1 %}
{% endfor %}
Comments