How to display a text if the field is empty ?

I tried the following code but it does not work :

    {% if content.field_description is not empty %}
      {{ content.field_description }}
    {% else %}
      test
    {% endif %}

Comments

zenimagine created an issue. See original summary.

gnanagowthaman sankar’s picture

Hi @zenimagine

Here by attached the code. Please let me know for any changes.

Thanks & Regards,
Gnanagowthaman sankar

gnanagowthaman sankar’s picture

Status: Active » Needs review

{% set foo = '' %}
{% set len = foo | length %}

{% if foo == '' or len == 0 %}
{{ 'empty field' }}
{% else %}
{{ 'not empty field' }}
{% endif %}

chi’s picture

Project: Twig Tweak » Drupal core
Version: 8.x-2.x-dev » 8.7.x-dev
Component: Documentation » theme system
Related issues: +#953034: [meta] Themes improperly check renderable arrays when determining visibility

The question does not deal with Twig Tweak module.

rohitreddygaddam’s picture

{% if content.field_description %}
  {{ content.field_description }}
{% else %}
  ...
{% endif %}

Version: 8.7.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

vikashsoni’s picture

Add the condition

{% if content.field_first_name %}
{{ content.field_first_name }}
{% else %}

{% endif %}

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
cilefen’s picture

Status: Needs review » Fixed

I am closing this support request because there have been no recent comments. It looks like a few people have offered answers. Thank you!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.