Twig's dump function doesn't attach symfony assets if templates use |render

Created on 16 May 2023, about 1 year ago
Updated 20 May 2023, about 1 year ago

Problem/Motivation

The symfony dump() function was added to twig, overriding twig's native var_dumper. In 10.1.0-beta, the output is not appropriately styled. This appears to be an issue related to how symfony sends along the necessary CSS/JS.

Additional discussion in slack

Steps to reproduce

Install the site using the demo_umami profile

Add a dump() call in twig, and note the output displays similarly to php's var_dump().

Looking at the inspector, we can see the sf-prefixed classes that indicate this is the symfony dumper.

If we add a dump() call in PHP, the CSS/JS are passed along correctly and the dump in twig displays correctly.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

10.1

Component
Theme 

Last updated 1 day ago

Created by

🇺🇸United States andy-blum Ohio, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @andy-blum
  • 🇺🇸United States andy-blum Ohio, USA
  • 🇺🇸United States andy-blum Ohio, USA
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    If I install 10.1.x demo_umami and and make the following change:

    diff --git a/core/profiles/demo_umami/themes/umami/templates/layout/page.html.twig b/core/profiles/demo_umami/themes/umami/templates/layout/page.html.twig
    index 5215326a3f2..054385ccccc 100644
    --- a/core/profiles/demo_umami/themes/umami/templates/layout/page.html.twig
    +++ b/core/profiles/demo_umami/themes/umami/templates/layout/page.html.twig
    @@ -52,6 +52,7 @@
               {{ page.pre_header }}
             {% endif %}
             {% if page.header|render|striptags|trim is not empty %}
    +          {{ dump() }}
               {{ page.header }}
             {% endif %}
           </div>
    

    Log in as admin and enable twig debugging in the UI (nice!) and then return to the frontend. It works as expected. The assets are attached and the var is dumped and I can expand sections and it's great. @andy-blum which template are you adding a dump to and which URL are you visiting?

  • 🇺🇸United States andy-blum Ohio, USA

    I'm editing the block--system-branding-block.html.twig file within the umami theme. The exact markup:

    {% block content %}
      {{ dump() }}
      {% if site_logo %}
        <a href="{{ path('<front>') }}" rel="home" class="site-logo">
          <img src="{{ site_logo }}" alt="{{ 'Home'|t }}" />
        </a>
      {% endif %}
      {% if site_name %}
        <div class="site-name">
          <a href="{{ path('<front>') }}" rel="home">{{ site_name }}</a>
        </div>
      {% endif %}
      {% if site_slogan %}
        <div class="site-slogan">{{ site_slogan }}</div>
      {% endif %}
    {% endblock %}
    

    It seems the exact combination that's breaking this for me is:

    • Twig debug mode: Enabled
    • Do not cache markup: Enabled

    The "Disable Twig cache" option does not seem to make a difference.

  • 🇺🇸United States andy-blum Ohio, USA

    If I move my {{ dump() }} to the page.html.twig file where Alex put his, the symfony debugger works.

  • 🇺🇸United States andy-blum Ohio, USA

    Perhaps it's related to something with blocks? block--bundle--banner-block.html.twig also prints out a broken dump

  • 🇫🇮Finland olli

    Looks like a problem with blocks in umami's pre_header and header regions: umami/templates/layout/page.html.twig renders those blocks twice ignoring the output of first |render where dump() sends the necessary CSS/JS.

  • 🇺🇸United States andy-blum Ohio, USA

    Confirming olli's comments - if I remove the uses of |render|striptags|trim then the dumps come through fine

  • 🇺🇸United States andy-blum Ohio, USA
  • 🇺🇸United States andy-blum Ohio, USA
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    Oh of course this is Umami's only render some divs if there are blocks code causing problems!

    I.e stuff like:

            {% if page.header|render|striptags|trim is not empty %}
              {{ page.header }}
            {% endif %}
    

    So... there are v old issues about how to determine if something is empty. Oh Nice @andy-blum has already related 🌱 [meta] Themes improperly check renderable arrays when determining visibility Needs work ... I think we should review the following code anyway...

      {% if page.pre_header| is not empty or
         page.header|render|striptags|trim is not empty %}
        <header class="layout-header" role="banner">
          <div class="container">
            {% if page.pre_header|render|striptags|trim is not empty %}
              {{ page.pre_header }}
            {% endif %}
            {% if page.header|render|striptags|trim is not empty %}
              {{ page.header }}
            {% endif %}
          </div>
        </header>
      {% endif %}
    

    as the multiple page.header|render|striptags|trim looks really wasteful even if internal optimisations in the render system mean it's not that bad.

    I think this issue is a duplicate of 🐛 Umami theme ignores placeholders and HTML replaced elements when checking for empty regions Needs work

Production build 0.69.0 2024