1

Having trouble seeing why my regions render when I think they shouldn't...

page.html.twig:

{% if page.region %}
  {{ page.region }}
{% endif %}

region.html.twig:

{% if content %}
  <div id="" aria-label="" role="" {{ attributes.addClass(region_classes) }}>
    {{ content }}
  </div>
{% endif %}

block.html.twig:

{% if content %}
<div{{ attributes.addClass(block_classes) }}>

  {# Label. If not displayed, we still provide it for screen readers. #}
  {% if not configuration.label_display %}
    {% set title_attributes = title_attributes.addClass('visually-hidden') %}
  {% endif %}
  {{ title_prefix }}
  <h2{{ title_attributes.setAttribute('id', heading_id) }}>{{ configuration.label }}</h2>
  {{ title_suffix }}

  <div>
    {% block content %}
    {{ content }}
    {% endblock %}
  </div>

</div>
{% endif %}

block--system-messages-block.html.twig:

{% extends "block.html.twig" %}

{% block content %}
{{ content }}
{% endblock %}

I believe it has to do with my extended block templates. These seem to be the ones that will still render even when there is "no" content.

What am I missing? If I use an if check around {% block content %} Twig throws an exception, inside there is no effect.

Adding |render|striptags|trim|length > 0 in any of these templates doesn't seem to solve the issue, and in some cases, forms and Drupal messages don't validate (so nothing returns when it should).

4
  • Unsure now as I've set both the Status messages block and Site branding block templates to have the same markup as block.html.twig (and don't extend any more) yet have the same results. The Help block, for example, works correctly, but I am not overriding that block with a custom theme template file. – Prestosaurus Apr 12 '19 at 5:16
  • 1
    There is an old issue for D7 drupal.org/node/953034, still open for D8. In this issue you can find workarounds for blocks depending on the page context, which is mainly the route/path of the page. There isn't a workaround for blocks like the status messages, because this block depends on session data. – 4k4 Apr 12 '19 at 6:54
  • Thanks, @4k4. It's a long thread so maybe missing something. The issue doesn't exist as long as my custom theme uses the stable theme template versions of region.html.twig and block--system-messages-block.html.twig:. Is that difference part of what the issue thread points to? – Prestosaurus Apr 12 '19 at 16:08
  • I very much doubt an empty check as proposed in your question or in the issue is possible for a region containing a message block (with any template). – 4k4 Apr 12 '19 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.