4

How can I print the title of my view in views-view.html.twig?

I have tried both {{ title }} and {{ label }} but neither work.

enter image description here enter image description here

You can see the title is set in the View, and the template is using {{ title }}

9
  • This must work. {{ title_prefix }} {{ title }} {{ title_suffix }} Are you sure you added a title to the view? Commented Jan 15, 2018 at 15:37
  • Yup, title is added to the view. I even removed my custom template, so the core theme template is used, and that doesn't show the title either. Commented Jan 15, 2018 at 15:40
  • You should be able to add in a {{ 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. Commented Jan 15, 2018 at 15:40
  • and did you flush all caches ? Commented Jan 15, 2018 at 15:44
  • Yup! Always do when working with templates :) Commented Jan 15, 2018 at 15:46

3 Answers 3

11

You have to use {{ view.title }}.

1
  • 1
    I can confirm this solution works for me. {{ title }} delivers empty content, while {{ view.title }} does the trick. Commented Mar 27, 2020 at 14:16
3

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.

1
  • 2
    There's a bug with this though -- it decodes encoded characters like "&" :(
    – atwixtor
    Commented Oct 29, 2018 at 3:59
2

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(),
  ];
}
2
  • Eurgh, what nonsense! what is the point of only showing the title in the preview? Will using the preprocess hook work with a multilingual setup? Commented Jan 15, 2018 at 17:04
  • This is because the title is displayed in a title block. I don't see why this shouldn't work in multilingual.
    – 4uk4
    Commented Jan 15, 2018 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.