I created a custom block named "Alert Bar" in the "navigation" region and I want to render it in my header file.

My header file currently has code like this to render the blocks -

{% if page.navigation or page.navigation_collapsible %}
  {% block navbar %}
    {% if no_head != 'true' %}
      {% include '@eventsns/components/navbar.html.twig' with incs %}
    {% endif %}
  {% endblock %}

Can I do something like {% block alertbar %} to display the alert bar block? The machine name says it is "alertbar" but I'm not sure if that's the correct variable since it does not render anything.

  • 1
    block in twig is not the same as block in drupal, if that's what you are thinking.
    – Kevin
    Commented Jan 27, 2021 at 23:37

2 Answers 2

  1. block in twig are a "placedholder" to re-use text. They aren't drupal blocks
  2. The easiest way as far I know to print a block is to use twig tweak module. The could should be {{ drupal_entity('block', 'block_id', check_access=false) }}

If you're rendering a custom block, you might want to use block_content instead.

  1. Block: {{ drupal_entity('block', 'block_id', check_access=false) }}
  2. Block Content: {{ drupal_entity('block_content', 'content_block_id') }}

Documentation: Rendering Blocks with Twig

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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