How do I get the page title for use in a theme? I'm using Drupal 8.

I've tried searching the Drupal web site and I looked at a few questions on here and I can't seem to figure it out.

Here is what I'm trying in my template_preprocess_page:

  $request = \Drupal::request();
  $route_match = \Drupal::routeMatch();
  $variables['page_title'] = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());

However, {{ page_title }} does not work. It seems as if the value is null.

  • It's not exactly a duplicate, because this is for the page template and in this template the title has moved to a block and you can put it in a region. If you want to access the title outside a region, it is still there: {{ page['#title'] }}. So no need for the code mentioned in the duplicate. – 4k4 Jan 31 '16 at 21:17

Browse other questions tagged or ask your own question.