简体   繁体   中英

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. 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

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.

{% 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 %} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM