每个公司对象都与图像具有一对多的关系.
现在在我的模板中,我想检查是否有类型为testtype的图像.
如何用树枝处理这个?以下是一个例外:
值"testtype"的意外标记"字符串"(预期"名称")
枝条
{% for image in company.images if image.type is 'testtype' %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
<ul>
{% for user in users if user.active %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
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 %}
Run Code Online (Sandbox Code Playgroud)
小智 5
你试过这个吗
{% for myimage in company.images %}
{% if myimage.type == 'testtype' %}
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9895 次 |
最近记录: |