I got the 2nd level main menu in a block in the sidebar, drupal 8 correctly hides the menu (and the block) when the user browses a page that is not in this menu, but the theme continues to show the empty region. The condition {% if page.region %} is true. is there any way to solve this problem?

  • First you should check whats inside page.region with {{ kint(page.region) }} (devel kint module enabled). It seems not to be empty. – Philipp Michael Apr 15 '16 at 8:16

It is better to use construction

{% if page.region|render|striptags|trim|length > 0 %} ... {% endif %}

So you can check if there is some content or not.

| improve this answer | |
  • 1
    Note: This will not work properly if a region contains only HTML tags, such as Search Facets that are completely made up of <select> options. The striptags call will remove everything from these regions, making them appear to be empty and preventing your facets from showing. – diamondsea Jun 12 '18 at 22:05

I you use menu_block module, this patch will fix the problem of a region showing up even when there are no subitems for a menu. Only tested on menu block version 8.x-1.5.

| improve this answer | |
  • your patch work nice vs the 8.x-1.5. Have you open an issue on the module page? – xaa Nov 8 '18 at 19:22

You can use {% if page.region|render|striptags %} to make sure the regions actually has content. See also the discussion here: [meta] Themes improperly check renderable arrays when determining visibility.

| improve this answer | |

I did it this way to reduce the number of renders.

{% set render_sidebar_first = page.sidebar_first|render %}
{% if render_sidebar_first and render_sidebar_first|striptags|trim|length > 0 %}
  {% block sidebar_first %}
    <aside class="{{ sidebar_class }}" role="complementary">
        {{ render_sidebar_first }}
  {% endblock %}
{% endif %}
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.