Skip to main content
Question Protected by CommunityBot
Tweeted twitter.com/StackDrupal/status/709471879837458433

Render views Programatically render a block insidein a twig template

deleted 47 characters in body; edited title
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Render views block inside twig template Drupal 8

I need to render a views block in my page.html.twig template. In D7 I would do this:

<?php
  $block = module_invoke('module_name', 'block_view', 'block_delta');
  print render($block['content']);
?>

In Drupal 8 module_invoke is deprecated and recommended to use this: (I've added the block's name as the second parameter)

Drupal::moduleHandler()->invoke($block, 'views_block__blog_block_1', $args = array());

I tried a few things. First tried to do it in a twig template but I don't know how to call php functions in a twig template, so that didn't turn out too well.

Then I called the function in the preprocess_page() function in the .theme file but before I could get it to work I tried something simpler just to try make a variable work inside the twig template which didn't work either, for example:

In the template_preprocess_page(&$vars) function in the .theme file:

$test = 'Hello World';
$vars['$my_var'] = $test;

I tried to call my_var inside the twig template but it didn't work and I got an error message saying "The site has an error, please contact the administrator"

So, to summarize, here are my questions:

  1. How do I make variables available inside twig templates?
  2. How do I call functions inside twig templates?
  3. Do I render blocks inside the .theme file or twig template?

Any help is greatly appreciated. Thank You.

Render views block inside twig template Drupal 8

I need to render a views block in my page.html.twig template. In D7 I would do this:

<?php
  $block = module_invoke('module_name', 'block_view', 'block_delta');
  print render($block['content']);
?>

In Drupal 8 module_invoke is deprecated and recommended to use this: (I've added the block's name as the second parameter)

Drupal::moduleHandler()->invoke($block, 'views_block__blog_block_1', $args = array());

I tried a few things. First tried to do it in a twig template but I don't know how to call php functions in a twig template, so that didn't turn out too well.

Then I called the function in the preprocess_page() function in the .theme file but before I could get it to work I tried something simpler just to try make a variable work inside the twig template which didn't work either, for example:

In the template_preprocess_page(&$vars) function in the .theme file:

$test = 'Hello World';
$vars['$my_var'] = $test;

I tried to call my_var inside the twig template but it didn't work and I got an error message saying "The site has an error, please contact the administrator"

So, to summarize, here are my questions:

  1. How do I make variables available inside twig templates?
  2. How do I call functions inside twig templates?
  3. Do I render blocks inside the .theme file or twig template?

Any help is greatly appreciated. Thank You.

Render views block inside twig template

I need to render a views block in my page.html.twig template. In D7 I would do this:

<?php
  $block = module_invoke('module_name', 'block_view', 'block_delta');
  print render($block['content']);
?>

In Drupal 8 module_invoke is deprecated and recommended to use this: (I've added the block's name as the second parameter)

Drupal::moduleHandler()->invoke($block, 'views_block__blog_block_1', $args = array());

I tried a few things. First tried to do it in a twig template but I don't know how to call php functions in a twig template, so that didn't turn out too well.

Then I called the function in the preprocess_page() function in the .theme file but before I could get it to work I tried something simpler just to try make a variable work inside the twig template which didn't work either, for example:

In the template_preprocess_page(&$vars) function in the .theme file:

$test = 'Hello World';
$vars['$my_var'] = $test;

I tried to call my_var inside the twig template but it didn't work and I got an error message saying "The site has an error, please contact the administrator"

So, to summarize, here are my questions:

  1. How do I make variables available inside twig templates?
  2. How do I call functions inside twig templates?
  3. Do I render blocks inside the .theme file or twig template?
edited tags
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
Source Link
Rick Bergmann
  • 808
  • 1
  • 9
  • 17
Loading