Print view header using the “Twig Tweak” module
Question
I would like to print out the header for a view, but not the rest of the content using the Twig Tweak module. I've tried variations on {{ drupal_view('VIEW_NAME', 'PAGE_NAME', 'view_header') }}
, but that prints out the entire view.
Solution
I have create simple extension for that:
- add the following to
MYMODULE.services.yml
services:
MYMODULE.twig.render_view_header:
class: Drupal\MYMODULE\Twig\HeaderView
tags:
- { name: twig.extension }
- Create
HeaderView
class insideMYMODULE/src/Twig
. - Copy the code from HeaderView.php
- Clear cache.
- In your twig call the extension like:
{{ render_view_header('VIEW_NAME', 'PAGE_NAME') }}
OTHER TIPS
Main twig template for the views is views-view.html.twig
. You can see header, footer, content , attachment variable in this file.So you can print your views in your method {{ drupal_view('VIEW_NAME', 'PAGE_NAME') }}
then rename views-view.html.twig
to views-view--VIEW_NAME--PAGE_NAME.html.twig
and remove unwanted content.
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange