Every company object is with a one-to-many relation with image.
Now in my template I want to check if there is an image of type testtype.
How to handle this with twig? The following gives me an exception:
Unexpected token "string" of value "testtype" ("name" expected)
Twig
{% for image in company.images if image.type is 'testtype' %}
{% endfor %}
The new way to do that is the following (for if
is deprecated since Twig 2.10):
{% for image in company.images|filter(image => image.type is 'testtype') %}
{% endfor %}
warning: this answer is already deprecated
<ul>
{% for user in users if user.active %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
http://twig.sensiolabs.org/doc/tags/for.html#adding-a-condition
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With