5

I'm trying to use the page title in my theme's page.html.twig. On normal pages, {{ node.label }} does the job, but that doesn't work on the front page. There, node is NULL, presumably because the font page is a view rather than a "real" page.

I have tried to dump all the available variables on page.html.twig using kint, but it seems that none of them contains the page title. Other questions discuss a similar matter, but either only for normal pages or for getting the title from PHP (and not from within a TWIG template).

I'm looking for an approach that works in any template, although page.html.twig is most important for me right now. It should also take a global title override into account. The ideal solution would also work on normal pages (i.e. not the front page).

TL;DR: I'm looking for a template variable/function X so that {{ X }} is the current page title, no matter whether I'm on the front page or on any other page.

  • front page title ??? what would that be? the website name? – No Sssweat Mar 7 '16 at 9:22
  • Can you explain the use-case in more details? Would a block with page title work for you? – Aram Boyajyan Mar 7 '16 at 9:24
  • @NoSssweat In my case it's the value I set in the global title override, but it should also work without a title override. – Florian Brucker Mar 7 '16 at 9:24
  • @AramBoyajyan: No, a block would not work for me. I need a solution that I can use directly in the template and to which I can apply the usual template tools (e.g. filters, conditionals, etc.). – Florian Brucker Mar 7 '16 at 9:26
11

You should really try using the blocks for this, but here's how to do what you asked:

function YOUR_THEME_preprocess_page(&$variables) {
  $request = \Drupal::request();
  $route_match = \Drupal::routeMatch();
  $page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
  $variables['current_page_title'] = $page_title;
}

This code should go in YOUR_THEME.theme file. You can use it then in page.html.twig this way:

{{ current_page_title }}

See this for more information.

  • This doesn't seem to work on the front page. The hook gets called and the variable is set, but on the front page it's just empty. Removing my title override doesn't change anything. On normal pages it works, but there I can just use node.label. – Florian Brucker Mar 7 '16 at 11:12
  • You need to set the title for front page. You can do that in: Admin -> Structure -> Views -> Frontpage -> Title. If you're using something else, you can add an if condition and fetch a different value for home page. – Aram Boyajyan Mar 7 '16 at 11:32
  • I have set a title for the front page. That's what I meant when I talk about "global title override" (that's how it's called in the settings page). But as I said: Your code doesn't seem to work no matter whether I have a front page title override or not. – Florian Brucker Mar 7 '16 at 12:00
  • Not sure I understand you; here's my screenshot with a marker showing where I changed the page title. Can you clarify? – Aram Boyajyan Mar 7 '16 at 12:09
  • Sorry, I mixed things up. I hadn't noticed that there is both a "normal" title setting and a title override setting in the front page settings (the question I've found on changing the title uses the title override). When I set a title via "your" setting then everything works. Thank you for your patience! – Florian Brucker Mar 7 '16 at 12:12
3
{{ page['#title'] }}

It seems to work on view, node and term pages.

  • 1
    Thank you for this, took me literally hours to track down... – JacobTheDev Sep 10 at 14:45

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.