Embed View in Drupal 8

As we are new to drupal 8 things are changed in Drupal 8 as compared to Drupal 7.

Today i am showing how to embed a view in block or in a view. It is very easy.

In drupal there is function :

views_embed_view($view_name, $display_id, $contextual_filter)

create a custom module, in .module file create your own function.

I created the following function:

function snehi() {
  $view_content = views_embed_view('banner', 'block_1');
  print render($view_content);

Where banner is my view name and block_1 is display id of my view.

In Drupal 7 we use drupal_render to render view this is not depreciated in 8 and will be removed in Drupal 9.

render function is used instead which is again a public function.

Here is the link for depreciation note:


Use this function in body of a cusotm block or in any field, where php is accepted as a text format.

Want to use the contextual filter, no worries use it as follow:

function snehi() {
  $view_content = views_embed_view('banner', 'block_1', '381');
  print render($view_content);


Where 381 is the value of contextual filter/ I used node id as contextual filter and passed the node id 381 into this function.