Hide block if no submenu in Twig I'm creating a node--page.html.twig and I'm using twig_tweak to help render a block for my sidebar to display the sub-menus. Is there a way to hide it if the page doesn't have any submenus?
Here's my code:
&lt;div class=&quot;page-sidebar grid-3 alpha&quot;&gt;
  &lt;div class=&quot;sub-region page-sidebar clearfix widgets widget-sidebar&quot;&gt;
    &lt;div class=&quot;contextual-links-region panel-pane pane-menu-tree pane-main-menu&quot;&gt;
    {{ drupal_entity('block', 'mainmenu', check_access=false) }}
  &lt;/div&gt;
&lt;/div&gt;

  
Is there some sort of if statement I could use to hide the block if the page has no submenu?
Thank you!
I generally handle this sort of thing like:
// First render my content and set it to a variable.
{% set submenu = drupal_entity('block', 'mainmenu', check_access=false)|render %}
// Next, check if the variable have content.
{% if submenu %}
  // If variable has content, render with wrapping div(s).
  &lt;div class=&quot;my-wrapping-div&quot;&gt;
    {{ submenu }}
  &lt;/div&gt;
{% endif %}

It's important to render the submenu whether or not it has any displayable content to make sure it's cache settings are bubbled up.
Note: this will only work if whatever your rendering returns no html. If you're seeing some wrapping divs, etc return, but no content, you can alter your if statement like {% if submenu|striptags|trim %} to strip html and check for only content.