Skip to main content
Key2Goal

Main navigation

  • Home
  • About Us
  • Contact us
  • Glossary
  • Privacy Policy
User account menu
  • Log in

Breadcrumb

  1. Home

How to render block in Drupal 8 theme template

Similar to Drupal 7, Drupal 8 also gives the customization using theme template, If you want to place a block using programmatically you have following options : 

1) Using the default Drupal method using THEME_preprocess_page,write this function in your mytheme.theme file, See the example below :

function THEME_preprocess_page(&$variables) {

// Load block
$block = \Drupal\block\Entity\Block::load('your_block_machine_name');
$variables['block_view'] = \Drupal::entityTypeManager()
  ->getViewBuilder('block')
  ->view($block);
  
//Get block title
$block_title = \Drupal\block\Entity\Block::load('main_menu')->label();

 $variables['title_of_block'] = $block_title;
 }

and put "block_view" & "title_of_block" as this format in twig template 

block title : {{title_of_block}}

block view : {{block_view}}

2) You can also try the easiest way using the Twig Blocks Module to make the variable directly, Just on the module and put the machine name like below:

 

 

{{ render_block('machine_name_of_your_block') }}

 

 

3) You can also try this module Twig Tweak, this also use to render views Blocks in theme 

 

 

{{ drupal_block('machine_name_of_your_block','block_1') }} 

 

 

Hope this helps you,

Thanks

  • Log in to post comments

Comments

Article category

  • Drupal
  • Wordpress
  • Java
  • Javascipt
  • HTML
  • CSS
  • IOS
  • Android
  • PHP
  • MySQL
  • Other
  • Command line
  • NodeJs
  • Drupal 8
  • Drupal 7
  • GIT
  • DevOps
  • Python
  • Xampp
  • Server
  • Artificial intelligence
  • AppScript

Recent content

  • Differences Between Bootstrap 3 & Bootstrap 4
    1 day ago
  • About Us
    1 day ago
  • Privacy Policy
    1 day ago
  • Create a Virtual Host on Xampp in 5 steps
    1 month 1 week ago
  • How to setup Multisite in drupal 9
    7 months 3 weeks ago
  • How to creating a FOR loop and a WHILE loop in JavaScript
    7 months 3 weeks ago
  • Calculator program HTML & PHP
    7 months 3 weeks ago
  • Algothrim to create a web application
    7 months 3 weeks ago
  • 10 Git Useful Commands for Every Developer
    7 months 3 weeks ago
  • Top 10 useful commands for GIT with example
    7 months 3 weeks ago
© Copyright 2017 - 2026 key2Goal. All Rights Reserved