How do you output views reference field views in Twig? I'm currently trying to output views referenced in a node (with Views Reference Field) inside page.html.twig Twig template.

I know I can just add or embed a view using {{ drupal_view('machine_name', 'block_1') }} (with Twig Tweak) in my Twig template. But I want the editors to have the flexibility to add or change views in the node and have them printed without writing code.

I can't seem to find any documentation on how to output a referenced view to my Twig template.
Got it!

You have to pass the view ID and the display ID to drupal_view() dynamically.

But first let me tell you that what you are doing, printing node stuff inside page level templates, should be considered bad practice. Good practice is to leave templates untouched, and to offer field formatters and other stuff instead that keeps the admin UI intact. So, when you get your first maintenance breakdown in a year or so, don't tell us we didn't warn you.

page.html.twig

{% if node.field_views and not node.field_views.isEmpty() %}
  {% for view in node.field_views.value %}
    {{ drupal_view(view['target_id'], view['display_id']) }}
  {% endfor %}
{% endif %}


You won't get the view title from here. Simply add some Global: unfiltered text to your view HEADER with any title markup. That will get printed.



drupal_view() requires Twig Tweak.