1

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 }}

  • This must work. {{ title_prefix }} {{ title }} {{ title_suffix }} Are you sure you added a title to the view? – Mohammed ATIFI Jan 15 '18 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. – Paul Canning Jan 15 '18 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. – Tyler Fahey Jan 15 '18 at 15:40
  • and did you flush all caches ? – Mohammed ATIFI Jan 15 '18 at 15:44
  • Yup! Always do when working with templates :) – Paul Canning Jan 15 '18 at 15:46
3

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

1

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(),
  ];
}
  • 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? – Paul Canning Jan 15 '18 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. – 4k4 Jan 15 '18 at 17:11
1

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
    There's a bug with this though -- it decodes encoded characters like "&" :( – atwixtor Oct 29 '18 at 3:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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