Skip to main content

Checking Empty Regions in Drupal 10 with twig

Sometimes we need to check if the page.some_region_a or page.some_region_b is empty and avoid the rendering of empty elements like empty divs (<div class="my-custom-theme-region"></div>)

Below are a few ways to check if that region is actually empty. 

Simple version (often enough):

{% if page.footer|render|striptags|trim is not empty %}
  <footer role="contentinfo">
    {{ page.footer }}
  </footer>
{% endif %}

Safer when HTML wrappers might fool the check:

{% if page.footer|render|striptags|trim is not empty %}
  <footer role="contentinfo">
    {{ page.footer }}
  </footer>
{% endif %}

For very correctness‑sensitive cases, avoid double-rendering:

{% set footer_rendered = page.footer|render %}
{% if footer_rendered|striptags|trim is not empty %}
  <footer role="contentinfo">
    {{ footer_rendered }}
  </footer>
{% endif %}

 
 

 

 
 
 
theming

Latest Articles

VSCode Drupal Coding Standards Setup (From Scratch) admin_pixelthi… Mon, 12/22/2025 - 16:01 VSCode Drupal Coding Standards Setup (From Scratch)

VSCode Drupal Coding Standards Setup (From Scratch)

vscode
Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file admin_pixelthi… Sun, 12/01/2024 - 16:49 Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file

Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file

composer
#1273 - Unknown collation: 'utf8mb4_0900_ai_ci' admin_pixelthi… Tue, 06/23/2020 - 13:21 #1273 - Unknown collation: 'utf8mb4_0900_ai_ci'

#1273 - Unknown collation: 'utf8mb4_0900_ai_ci'

utf8mb4