How can I print the title of my view in views-view.html.twig?
I have tried both {{ title }}
and {{ label }}
but neither work.
You can see the title is set in the View, and the template is using {{ title }}
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityYou have to use {{ view.title }}
.
{{ title }}
delivers empty content, while {{ view.title }}
does the trick.
Mar 27, 2020 at 14:16
The title is only available in the admin preview, see the documentation of the template:
views-view.html.twig
{#
/**
* @file
* Default theme implementation for main view template.
*
* Available variables:
...
* - title: Title of the view, only used when displaying in the admin preview.
In other cases add it in preprocess:
mytheme.theme
/**
* Implements hook_preprocess_HOOK() for views templates.
*/
function mytheme_preprocess_views_view(&$variables) {
$view = $variables['view'];
$variables['title'] = [
'#markup' => $view->getTitle(),
];
}
An easy workaround is to add a "Global: Text area" field in the view header and use :
<h1>[view:title]</h1>
This will do the trick in most cases if you don't use the header for something else.
{{ dump() }}
debug statement in your twig file, and at least get an idea of what variable are there for you to use. See drupal.org/docs/8/theming/twig/debugging-twig-templates for full details on setting up twig debugging.