Twig Real Content - empty region / content check helper

Provides a Twig filter and test to determine if a Twig variable (typically a region) contains meaningful content or is effectively empty.

twig_real_content
2,335 sites
20
drupal.org

Install

Drupal 11, 10, 9 v1.0.2
composer require 'drupal/twig_real_content:^1.0'

Overview

Twig Real Content solves a common Drupal theming problem: determining whether a region or rendered variable actually contains visible content versus just empty wrapper markup. In Drupal's rendering system, regions and content areas often contain HTML wrapper elements even when there's no actual content to display, making it difficult to conditionally hide empty sections in templates.

This module provides both a Twig filter and a Twig test called real_content that strips HTML tags (while preserving meaningful elements like images, videos, and iframes) and checks if any actual content remains. This allows theme developers to properly show or hide regions based on whether they contain real, visible content.

The module addresses the long-standing Drupal core issue #953034 regarding the difficulty of checking for empty regions in Twig templates.

Features

  • Provides a 'real_content' Twig test that returns TRUE if a variable contains meaningful content, FALSE if empty
  • Provides a 'real_content' Twig filter that returns stripped content for further processing
  • Intelligently preserves content-bearing elements (img, video, iframe, embed, script, style, etc.) while stripping empty wrapper markup
  • Handles Drupal render placeholders correctly to avoid false negatives with lazy-loaded content
  • Works with rendered strings and MarkupInterface objects
  • Throws helpful exceptions when unrendered arrays are passed, guiding developers to proper usage
  • Compatible with Drupal 9, 10, and 11

Use Cases

Conditionally display regions with real content

Use the real_content test in your page.html.twig to only display region wrappers when they contain actual content: {% if page.sidebar_first|render is real_content %}<aside class="sidebar">{{ page.sidebar_first }}</aside>{% endif %}

Hide empty content sections

When building custom layouts, use real_content to avoid showing empty sections: {% if content.field_body|render is real_content %}<div class="body-content">{{ content.field_body }}</div>{% endif %}

Clean up region output for processing

Use the real_content filter to get clean content for further processing: {% set clean_content = page.sidebar_first|render|real_content %}

Prevent empty wrapper divs in Views output

When theming Views, use real_content to ensure you don't output wrapper markup for empty view results or fields.

Tips

  • Always render the variable before using the real_content test or filter. Use the pattern: {{ variable|render is real_content }}
  • The module expects rendered strings - unrendered arrays will throw a TwigRealContentException with a helpful message
  • Media elements like images, videos, iframes, and embeds are considered 'real content' even though they are self-closing tags
  • Drupal render placeholders are preserved so that lazy-loaded content is not incorrectly identified as empty
  • For complex content checking needs, combine with the Twig Capture module for more control over when rendering occurs

Technical Details

Troubleshooting 3
TwigRealContentException: expects rendered strings as value, array given

The variable passed to real_content was not rendered. Apply the |render filter first: {{ my_variable|render is real_content }}

Region appears empty but contains JavaScript or CSS

Script and style tags are preserved as 'real content'. If you want to ignore them, you may need custom logic or to process the content differently.

Lazy-loaded content incorrectly marked as empty

The module preserves <drupal-render-placeholder> tags specifically to handle lazy rendering. Ensure you're using the module's filter/test after the variable is rendered.