How can I get an empty region not to render in a field_layout template? I've got a layout_discovery template set up like this, for use in a field layout:

{% if content %}
&lt;div{{ attributes.addClass(classes) }}&gt;
  &lt;div class="layout-region layout-region--main col-sm-8"&gt;
    {{ content.main }}
  &lt;/div&gt;

  {% if content.sidebar %}
  &lt;div class="layout-region layout-region--sidebar col-sm-4"&gt;
    {{ content.sidebar }}
  &lt;/div&gt;
  {% endif %}
&lt;/div&gt;
{% endif %}


(I have also tried {% if content.sidebar|trim|length &gt; 0 %} with no change in the result.)

A content type using this layout puts a single, optional field into the sidebar region. If that field is empty, I would expect content.sidebar to be empty, so that the sidebar div won't be output and the main div can expand to fill the space. However, instead I get an empty sidebar div:

&lt;div class="layout-region layout-region--sidebar col-sm-4"&gt;
&lt;/div&gt;


Is there any way to accomplish this in a field layout, or do I need to build a custom node template instead?
Use this syntax:

{% if content.sidebar %}
  &lt;div class="layout-region layout-region--sidebar col-sm-4"&gt;
    {{ content.sidebar }}
 &lt;/div&gt;
{% endif %}


It will remove all HTML (such as the s that appear even if the section is empty and then only display if there is some non-HTML present.

NOTE: This will not work properly if a region contains only HTML tags, such as Search Facets that are completely made up of  options. The striptags call will remove everything from these regions, making them appear to be empty and preventing your facets from showing

See also the discussion here: [meta] Themes improperly check renderable arrays when determining visibility.