首页 > 解决方案 > 带树枝的条件显示

问题描述

我在视图中使用 Twig 以条件重写输出。

{{ field_illus_lycee }}
{% if field_titre_pour_views is defined %}
    {% if field_titre_pour_views is not empty %} 
        {{ field_titre_pour_views }}
    {% endif %}
{% else %}
    {{ title }}
{% endif %}  
<span class="accroche-admin">{{ body }}</span>

我只想显示field_titre_pour_views它存在并且不为空,否则应该显示常规标题。但此时不显示常规标题。受此启发

我不明白我犯了哪个错误。

编辑:正确的代码

 {{ field_illus_lycee }}
        {% if field_titre_pour_views is defined %}
            {% if field_titre_pour_views is not empty %} 
                {{ field_titre_pour_views }}
{% else %}
            {{ title }}
            {% endif %}
        {% else %}
            {{ title }}
        {% endif %}  
        <span class="accroche-admin">{{ body }}</span>

标签: twigdrupal-8drupal-views

解决方案


有时,要问就是找到......这段代码可以解决问题:

 {% if field_titre_pour_views |default %}
     {{ field_titre_pour_views }}
 {% else %}
     {{ title }}
 {% endif %}

自动修复:) 希望它会帮助别人。


推荐阅读