not sure if this should be a bug report but let's start with a support request :)

Currently, we have to use the following code for conditionals based fields:

{% if content.field_more_infos['#items'] is not empty %}
  <div class="my-field-wrapper">
    {{ content.field_more_infos }}
{% endif %}

would be much nicer if this worked intuitively:

{% if content.field_more_infos %}
  <div class="my-field-wrapper">
    {{ content.field_more_infos }}
{% endif %}


dasjo created an issue. See original summary.

dasjo’s picture

When we figure out the proper way to handle this, let's add it to the docs:

Cottser’s picture

joelpittet’s picture

Status: Active » Closed (duplicate)

I think @Cottser is correct.

Try this.

{% if content.field_more_infos|render %}
{{ content.field_more_infos }}
{% endif %}


{% set more_infos =  content.field_more_infos|render %}
{% if more_infos %}
{{ more_infos }}
{% endif %}
joelpittet’s picture

Also there is no really good way to deal with this problem at the moment in Twig or PHP template because it's hard to know what makes a render array truely empty and performance implications. Please continue the discussion in that issue referenced in #3

jwilson3’s picture

I'm finding that |render is not enough due to random whitespace issues from field templates, but the following works:

{% if content.field_more_infos|render|trim is not empty %}
    {{ content.field_more_infos }}
{% endif %}

the "is not empty" part may not be necessary, but |render|trim is absolutely needed.

Chi’s picture

@jwilson3 here is the trick for views. I suppose it works for fields as well.

hkirsman’s picture

Tx, works for me at the moment in ds-2col-stacked.html.twig:

{%  if left|render|trim %}
    <{{ left_wrapper }}{{ left_attributes.addClass('group-left') }}>
      {{ left }}
    </{{ left_wrapper }}>
  {% endif %}
autopoietic’s picture

In case it helps someone else, I found that {% if content.field_myfield|render|trim is not empty %} does not work as expected if twig debug is enabled, because template suggestions are still returned when the field is otherwise empty.

natemow’s picture

Use "striptags" to remove Twig debug comments: {% if content.field_myfield|render|striptags|trim is not empty %}

ArchieV’s picture

Using "|render", "|strip_tags" is only good if you have your output properly cached, and it still might not cover all edge cases.

From what I can see here - it's a entity template.
In this case you can get values directly from entity (which in most cases available in template). So if you need to print, let's say, field_date on your node page, you can do something like:

{% if node.field_date.value %}
  <div class="my-field-wrapper">
    {{ content.field_date }}
{% endif %}
hmartens@gmail.com’s picture

Thank you so much for your input ArchieV! This worked for me. I used {% if my_paragraph_field.value %} and that worked perfect for me!

This works so much better than guessing what you should keep adding like |strip_tags|tags|sotiredofguessingthetagthatwillwork

hmartens@gmail.com’s picture

I see that in one twig I just have to use {% if not banner_image %} and on another twig file it doesn't work...is it just me or is it flaky?

Some settings on the images.

rominronin’s picture

In response to #11, the .value suffix may work if you have access to node, this is not always the case (this returns NULL on a display suite template, where I'm having difficulty checking for empty regions).

I didn't want to sound negative when I hit the comment button, but now that I think about it; the benefits offered by twig that attracted the Drupal decision makers to it are not so easy to pass on to an important group of Drupal users: themers, newcomers, template builders.

It's great that there is a workaround like 'node.field.value' for *some* use cases, but then we need to have Drupal specific documentation for twig. I'm happy with the compromises since I know Drupal well enough to know several other ways to get the markup I need, but a newcomer has maybe no hope without a holding hand.

ArchieV’s picture

To #14.

No, it's not *some* use cases and it's not a "workaround". Doing "render|striptags|trim" - is workaround.

This works when you use native templates for most of entities: users, nodes, paragraphs, etc. which is pretty common task and building castles with twig filters is not a good idea when you can do same stuff much in more obvious and bulletproof maner.

"Drupal specific documentation for twig" - link, you are welcome. Drupal already expands twig a lot, so you cannot avoid learning "drupal specific" twig.

"Newcomer has maybe no hope without a holding hand" - we all sitting here to help newcomers to do stuff in best, the most efficient way possible. Answering questions right with covering all possible ways is one of the ways to do that.