Tom Tom - 1 year ago 387
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 Source

You can use for loop in twig:

{% set activeMailCount = 0 %}
{% for mail in account.mailList if %}
    {% set activeMailCount = activeMailCount + 1 %}
{% endfor %}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download