Templates 如果字段为空,如何显示文本?
如果字段为空,如何显示文本 我尝试了以下代码,但不起作用:Templates 如果字段为空,如何显示文本?,templates,drupal,twig,drupal-8,Templates,Drupal,Twig,Drupal 8,如果字段为空,如何显示文本 我尝试了以下代码,但不起作用: {% if content.field_description is not empty %} {{ content.field_description }} {% else %} test {% endif %} 当字段为空时,它不会随内容变量一起出现 因此,您可以通过isset进行简单检查 下面的内容对我很有用: {% if content['field_description']
{% if content.field_description is not empty %}
{{ content.field_description }}
{% else %}
test
{% endif %}
当字段为空时,它不会随内容变量一起出现 因此,您可以通过isset进行简单检查
下面的内容对我很有用:
{% if content['field_description'] IS NOT EMPTY %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
请注意,它可能因您处理的内容类型和字段而异。如果您安装了Twig Tweak,您可以执行以下操作:
{% if content['field_description'] | field_value != '' %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}
什么不起作用?应该按原样工作,谢谢你,我刚刚测试过,但它不工作。如果字段为空,则不会显示替换文本{%If content.field_description%}{{content.field_description}{%else%}Quel dommage,ce membre n'a malheurement pas encore complétésa présentation。这个问题是同一个问题的主要问题,我希望你能找到你的答案。
{% if content['field_description'] | field_value != '' %}
{{ content['field_description'].value | striptags }}
{% else %}
<p class="des-empty">test</p>
{% endif %}