How to tell if a field has renderable content before printing HTML around it? I have a half dozen reference fields on a content type that sits at the bottom of a page under a heading of "Related" and some markup structure. I was checking if node.field_name is not empty, ... in twig before entering and printing "Related" and then each individual field, but in cases where the referenced entities are unpublished, this results in just "Related" and nothing else.

How can I figure out ahead of time if the field I am about to render has actual accessible content to show the user? is content.field_name|render|striptags|trim still the only way?

&lt;div class="info-grid {% if horizontal %}info-grid--horizontal{% endif %}"&gt;
    &lt;h2 class="info-grid__title"&gt;Related&lt;/h2&gt;
    &lt;div class="info-grid__grid"&gt;
      {% if node.field_topics is not empty  %}
        {{ content.field_topics }}
      {% endif %}

      {% if node.field_programs is not empty  %}
        {{ content.field_programs }}
      {% endif %}

      {% if node.field_working_groups is not empty  %}
        {{ content.field_working_groups }}
      {% endif %}

      {% if node.field_projects is not empty  %}
        {{ content.field_projects }}
      {% endif %}

      {% if node.field_papers is not empty  %}
        {{ content.field_papers }}
      {% endif %}

      {% if node.field_datasets is not empty  %}
        {{ content.field_datasets }}
      {% endif %}
    &lt;/div&gt;
  &lt;/div&gt;


Edit: updating with Hudri suggestion, it still prints the h2 of Related:

{% set display_related = false %}

{% if not (node.field_programs.isEmpty == true) and content.field_programs is defined %}
  {% set display_related = true %}
{% endif %}

{% if display_related %}
  &lt;div class="info-grid {% if horizontal %}info-grid--horizontal{% endif %}"&gt;
...
The answers so far are only valid for non reference fields. A reference field is different, because the referenced entities can have different access results than the parent entity. The rendered field can be empty although the field in the node object is not. As explained in How do I hide an empty field if my custom field formatter removes all items? there are two unambiguous empty conditions for the output of the field formatter in {{ content.field_name }}:


Check the first field delta, which always starts with 0:

{% set display_related = content.field_name.0 is defined %}

or check whether the field formatter could find any renderable children and has added the field wrapper:

{% set display_related = content.field_name['#theme'] is defined %}



For a reference field it is important to render content.field_name even if the result is negative, because it contains the cacheable metadata of the access checks. When for example a referenced node is published, the parent node needs to be invalidated and rerendered.

So checking content.field_name|render|striptags|trim is not so bad at all, because this check renders the field content and if the result is negative you don't have to render the field again, because the metadata bubbled up already.



Field Groups

For halve a dozen fields you could use Field Groups. Place all reference fields in a group "Related" and display the header in the field group template. If the field group template doesn't hide automatically you can check with striptags as before. You get the entire rendered result of all fields in the variable children.

Although I would prefer to check the specific content you expect to be inside of the field group and so ignore labels or other things that may be added later. The already rendered field group content is in a markup object and you need to render it again in twig, so you can check it casted as a string. For example whether it contains a linked node:

field-group-html-element.html.twig:

{% set display_related = '&lt;a href' in children|render %}