From the deprecated page of Twig documentation:

Adding an if condition on a for tag is deprecated in Twig 2.10. Use a filter filter or an "if" condition inside the "for" body instead (if your condition depends on a variable updated inside the loop)

If it's clear to me the part:

an "if" condition inside the "for" body

In this way:

<ul>
    {% for user in users if user.active %}
        <li>{{ user.username|e }}</li>
    {% endfor %}
</ul>

Instead this part it's not clear:

Use a filter filter

Any example explaining how doing it wrong and doing it right?

1 Answers

0
Edi Modrić On Best Solutions

It should be this way (taken from the docs):

<ul>
    {% for user in users|filter(user => user.active) %}
        <li>{{ user.username|e }}</li>
    {% endfor %}
</ul>