Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 如果字段为空,如何显示文本?_Templates_Drupal_Twig_Drupal 8 - Fatal编程技术网

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