【问题标题】:Conditionnal display with twig带树枝的条件显示
【发布时间】:2020-01-26 19:17:08
【问题描述】:

我在 Views 中使用 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 存在且不为空时才显示它,否则应显示常规标题。但此时不显示常规标题。灵感来自this

我不明白我犯了什么错误。

编辑:正确的代码

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

【问题讨论】:

    标签: twig drupal-8 drupal-views


    【解决方案1】:

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

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

    自动修复:) 希望对其他人有所帮助。

    【讨论】:

    • 另请注意,这是因为您的第二个条件 {% if field_titre_pour_views is not empty %} 没有 else 语句。您处于该字段实际定义但为空的情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多