Make Twig debug comments optional for empty markup.

Created on 10 November 2023, about 1 year ago
Updated 4 August 2024, 5 months ago

Problem/Motivation

When twig debug is enabled, Twig engine does not care whether a rendered template has any output or not. Even for an empty markup (an empty string "") it still adds all the comments about theme suggestions and template files in use. This makes it hard to deal with empty elements while in dev mode - e.g. check if a template variable is empty. Developers find various ways to deal with it, but the reality is - this is only happening when in debug mode, and therefore needs to be addressed on debug-level.

This happens when a variable might or might not contain a rendered markup, and when that variable is checked for empty content. E.g. a sidebar_first variable.

Steps to reproduce

1. Enable Twig debug in your services file:

parameters:
  twig.config: { debug: true, auto_reload: true, cache: false }

2. Create a block plugin that returns an empty array - in other words has no content.
3. Place that block in a region and inspect the page source in dev tool.

You can still see output - twig debug comments, while the region is not supposed to have any output at all.

Proposed resolution

- Implement an optional check for empty markup on twig engine level,
- Implement a twig debug option, which would control the above check.

The option could look like:

parameters:
  twig.config: { debug: true, auto_reload: true, cache: false, debug_on_empty: false }
                                                                                           ^^

For backward compatibility, defaults to printing debug info for empty markup.

Remaining tasks

Review/discuss proposed solution.

User interface changes

None

API changes

Adds an option to twig debug configuration.
For backward compatibility, disables twig debug comments for empty markup only when specifically instructed so.

Data model changes

None

✨ Feature request
Status

Needs work

Version

11.0 🔥

Component
Theme  →

Last updated 4 days ago

Created by

🇺🇸United States euk

Live updates comments and jobs are added and updated live.
  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024