Render block content in a Twig template

Public

themename.theme

Get raw version
php
  1. /**
  2.  * Implements theme_preprocess_example_hook().
  3.  */
  4. function themename_preprocess_example_hook(&$variables) {
  5.  
  6. // Create a variable for the block w/ ID "example_block_id".
  7. $block = \Drupal\block\Entity\Block::load('example_block_id');
  8. $variables['example_block'] = \Drupal::entityTypeManager()
  9. ->getViewBuilder('block')
  10. ->view($block);
  11.  
  12. }

example_hook.html.twig

Get raw version
php
  1. {{ example_block }}