This question already has an answer here:

In drupal 8 I want to print the block programatically. For this I have tried the below code but not working.

function theme_preprocess_page(&$variables) {
$block = \Drupal\block\Entity\Block::load('your_block_id');
$variables['block_output'] = \Drupal::entityTypeManager()

In page.html.twig I have added {{ page.block_output }}. But block is not rendered. Is there any other way to do this. Without using contrib modules.

marked as duplicate by No Sssweat, Yusef Mohamadi, Krishna Mohan, Adrian Cid Almaguer, kenorb Jul 21 '16 at 8:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • is your_block_id your custom block or block from contributes or core modules? – Yusef Mohamadi Jul 18 '16 at 9:20
  • Block from contrib module not custom block. @zhilevan – Vamsi Jul 18 '16 at 9:38
  • 1
    Can we re-open this? It's actually not a duplicate after all, since here it wants to print a normal block and not a view block. – No Sssweat Oct 19 '17 at 0:09

To render contributor module block's

function theme_preprocess_page(&$variables) {
  $block = \Drupal\block\Entity\Block::load('simplenewssubscription');
  $block_content = \Drupal::entityTypeManager()
 $variables['youblockname'] = $block_content;

and in your theme file just use

{{ youblockname }}

it seems you do it correct and just need use block_output instead page.block_output .

and rebuild the cache

  • Note that this will make the variable your_block_name available in the page.html.twig . For other template files use the appropriate preprocess hook. E.g. theme_preprocess_node for node.html.twig files – batigolix Jun 14 '18 at 9:29

Don't reinvent the wheel, you can use Twig tweak

Twig Tweak module provides a Twig extension with some useful functions and filters that can improve development experience.

Ex: <dd>{{ drupal_block('bartik_powered') }}</dd>

  • 3
    This has just made me very happy! – tanc Oct 19 '16 at 10:14
  • 1
    Fast and easy solution. – Ajay Gadhavana Jul 6 '17 at 11:59
  • 2
    I'm trying to render the default search block (mytheme_search) and this is not working for that. Any idea? – ProGrammar Aug 22 '17 at 21:49

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