twig - 带树枝的条件显示
问题描述
我在视图中使用 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>
解决方案
有时,要问就是找到......这段代码可以解决问题:
{% if field_titre_pour_views |default %}
{{ field_titre_pour_views }}
{% else %}
{{ title }}
{% endif %}
自动修复:) 希望它会帮助别人。
推荐阅读
- javascript - 具有动态生成内容的闭包
- configuration - 强制 ORIENTDB_HOME 和 ORIENTDB_DIR
- ocaml - 如何在ocaml中将二进制代码与源代码分开?
- c# - C#如何识别两个对象之间的碰撞
- css - 用本地 css 替换全局 css
- ssl - 从 IBM JRE 1.6 连接到 SOAP 服务时出现问题
- java - 整数无法转换为 JSONObject
- java - 水平滚动条不适用于 JScrollPane
- azure-cognitive-search - 在天蓝色搜索中按多个字段分组/分面
- python - 用于检查特定键是否存在于列表和字典的字典中的 Python 代码