每个公司对象都与图像具有一对多的关系。
现在在我的模板中,我想检查是否有类型为 testtype 的图像。
如何用树枝处理这个?以下给了我一个例外:
值“testtype”的意外令牌“字符串”(预期为“名称”)
枝条
{% for image in company.images if image.type is 'testtype' %}
{% endfor %}
警告:此答案已被弃用
<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
执行此操作的新方法如下(for if
自 Twig 2.10 起已弃用):
{% for image in company.images|filter(image => image.type is 'testtype') %}
{% endfor %}
你试过这个吗
{% for myimage in company.images %}
{% if myimage.type == 'testtype' %}
{% endif %}
{% endfor %}
只要testtype
是一个字符串,那么我会尝试:
{% for image in company.images if image.type == 'testtype' %}