in my theme template of page.html.twig (theme is a subtheme of classy core theme), my if statements to render a region or not are completely ignored by twig template engine. The regions (e.g. the first sidebar are always rendered.

Code of my page.html.twig:

  <header role="banner">
    {{ page.header }}

  {{ page.primary_menu }}
  {{ page.secondary_menu }}

      {% if page.navigation %}
      <nav class="layout-navigation" role="navigation">
        {{ page.navigation }}
    {% endif %}

  {{ page.breadcrumb }}

  {% if page.system_messages_area %}

    {{ page.system_messages_area }}

  {% endif %}

  {{ page.highlighted }}

  {{ page.help }}

  <div id="main-wrapper">
    <main role="main">
      <a id="main-content" tabindex="-1"></a>{# link is in html.html.twig #}

      <div class="layout-content">
        {{ page.content }}
      </div>{# /.layout-content #}

      {% if page.sidebar_first %}
        <aside class="layout-sidebar-first sidebar" role="complementary">
          {{ page.sidebar_first }}
      {% endif %}

      {% if page.sidebar_second %}
        <aside class="layout-sidebar-second sidebar" role="complementary">
          {{ page.sidebar_second }}
      {% endif %}


  {% if page.footer %}
    <footer role="contentinfo">
      {{ page.footer }}
  {% endif %}

Code of my theme.info.yml:

name: mytheme
type: theme
description: Based on classy core base theme.
core: 8.x
# Defines the base theme
base theme: classy
# Defines libraries group in which we can add css/js.
logo: logo.png
  - mytheme/global-styling
# Regions
  header: Header
  navigation: Navigation and Menus
  system_messages_area: Area for system messages 
  highlighted: Featured
  help: Help
  content: Content
  sidebar_first: First sidebar
  sidebar_second: Second sidebar
  footer: Footer

Maybe someone could help me to find the mistake, as far as I can see everything is at it's right place.

Thanks in advance

Best regards phil

  • I am going to ask 2 silly questions here. 1 - Did you clear the cache? 2 - Did you actually add a block to the sidebar_second region? – Stephen.R Mar 13 at 23:25
  • Also if it is a block generating a menu, depending on how, it needs both menu links and an active trail – Kevin Mar 13 at 23:31
  • To answer the questions: 1. I cleared the caches multiple times. 2. No I didn't add a block to the sidebar_second region. The region is rendered as an empty div-container. – dercheffe Mar 14 at 5:32

I would check, if the page render-array contains the element sidebar_first. You can do this with

{{ dump( page|keys) }}

in your page.html.twig (or use devels kint function for that). The |keys filter will prevent you from memory overflow if your page is big.

If this is the case, then your template is ok and you don't have to look at twig, but to search, what else is in the render array and where it could be originated.

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.