[英]Symfony : “if” does not work in a twig template
I'm trying to validate the size of an array before print a value, but the if instruction doesn't work. 我正在尝试在打印值之前验证数组的大小,但是if指令不起作用。 Always pass thought the if.
总是以为如果。
This is my code: 这是我的代码:
{% set size = custodian.phoneNumbers|length %}
{% if size > 3 %}
{% block phone_number3 %}{{phoneNumbers[2].phoneNumber }}{% endblock %}
{% endif %}
size is equal to 2 大小等于2
I try with this code and does not work as well. 我尝试使用此代码,但效果不佳。
{% set size = true %}
{% if size == false %}
{{size}}
{% endif %}
Please help!!! 请帮忙!!! Thanks in advance.
提前致谢。
I found the answers myself 我自己找到了答案
The block should be outside of the if. 该块应该在if之外。
{% block phone_type3 %}
{% if size >= 3 %}
{{ custodian.phoneNumbers[2].phoneType.value }}:
{% else %}
:
{% endif %}
{% endblock %}
{% block phone_number3 %}
{% if size >= 3 %}
<b>{{ custodian.phoneNumbers[2].phoneNumber }}</b>
{% endif %}
{% endblock %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.