2

The following line in page.html.twig does not print the page title:

<h1>{{ title }}</h1>

The page title outputs using the Page title block, but not this piece of template code. I found the title variable in the documentation commented out at the top of page.html.twig. Does this code need to be wrapped in something else?

2 Answers 2

2

The variable {{ title }} is only used in maintenance and install pages where blocks are not available. See the change record https://www.drupal.org/node/2568563

2
  • So is there any other way of printing the page title in page.html.twig instead of using the block? If the block is the only way, what would this look like in page.html.twig?
    – crayden
    Commented Jan 30, 2020 at 19:26
  • You move the title block to a region and print this region in page.html.twig.
    – 4uk4
    Commented Jan 30, 2020 at 19:47
2

Sometimes your overall site architecture might have Page Title block settings using content types or other configuration which might leave one-off cases where this doesn't allow the rendering of the block via a given region.

You can render the page title with:

<h1>{{ drupal_title() }}</h1>

But much better... You can call the page title block in your page.html.twig template this way:

{{ drupal_block('page_title_block') }}

This will render the Page Title block (which works with translations).


Small note that printing the Page Title block this way can have a different array structure than rendering the page title block via a region, so if you are applying a page title alter somewhere, for example, you may have to take this into consideration.

function MYMODULE_preprocess_page_title(&$variables) {
  if (array_key_exists('#markup', $variables['title'])) {
    $variables['title']['#markup'] = $value;
  }
  else if (isset($variables['title'])) {
    $variables['title'] = $value;
  }
}

But of course, you can place another instance of the Page Title block via the block admin form and configure unique display settings, as well as place it in a custom region if needed.

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.