How to render two views in the same twig template I want to render two views in the same twig template, but only one of them is rendered, and the second is always override with the same content as the first one.
I have a content type 'contacts' and when its nodes are displayed (full view) I want to render two views in the same page after the node fields. I use a twig template node--contacts.html.twig :

.....  node fields twig template .... 
{{ viewCalls }}  
....  
{{ viewAppointments }}  
....


And my function my_theme_preprocess_node(&amp;$variables) in my .theme file:

....  
$view = \Drupal\views\Views::getView('view_calls');
$view-&gt;setDisplay('default');
$view-&gt;setArguments(array($variables['node']-&gt;id()));
$view-&gt;setOffset(0);
$view-&gt;execute();
$variables['viewCalls'] = $view-&gt;render();

$view2 = \Drupal\views\Views::getView('view_appointments');
$view2-&gt;setDisplay('default');
$view2-&gt;setArguments(array($variables['node']-&gt;id()));
$view2-&gt;setOffset(0);
$view2-&gt;execute();
$variables['viewAppointments'] = $view2-&gt;render();
.....


As I said, the second view always shows the same content as the first. If I reverse the order of rendering, then the content is reverse too, and the one which is first rendered is the only one I see, and always two times. It is as {{ viewCalls }} and {{ viewAppointments }} always get the same content.
The reason you get the same content is due to the #cache property of the render array you get when you call the render() method.

Most specifically, by calling setOffset() earlier in your code, you are implicitly setting a cache key on the render array, (see here):


$this-&gt;element['#cache']['keys'][] = 'offset:' . $offset;



This cache key setting, in combination with the #theme property of the render array (views_view), which is the same for both views, will result in getting a cached version of the first one when including the second one, since the [#theme] + [#cache][keys] combination is identical for both views.

To work around that you could:


Remove caching completely for these views, or
Avoid using setOffset(), or
Add more specific keys to the cache so that you have a different cache for each view. E.g.

$variables['viewCalls']['#cache']['keys'] = 
  array_merge(
    $variables['viewCalls']['#cache']['keys'], 
    array('viewCalls')
  );
...
$variables['viewAppointments']['#cache']['keys'] = 
  array_merge(
    $variables['viewAppointments']['#cache']['keys'],
    array('viewAppointments')
  );



I tested your case using the 3. option and it worked as expected, I think before resolving to using another module, you could try sticking to your original code and appropriately modifying the #cache property of both render arrays.

Good luck!