I am using a custom theme with Stable as a base.

I am trying to figure out how to set the <title> tag and I'm having a hard time.

I noticed in html.html.twig there is the following:

<head-placeholder token="{{ placeholder_token|raw }}">

How does the page title find it's way into the above?

  • why does it matter? it's magic :D
    – user21641
    Jul 3 '17 at 14:27
  • Sure, but at the moment it's a mystery. I can't figure out how to set the title tag of my site's front page. How do you use this magic? Jul 3 '17 at 14:30
  • Where and how do you want to set the title? The title is functionality, defined by the route/controller of the request and should automatically be added to the page. Why would you want to change that from your theme?
    – Berdir
    Jul 3 '17 at 20:35
  • @Berdir, Thanks, I will check out routes and controllers. Jul 3 '17 at 21:51

I found a way but I'm not sure if it's ideal:

function THEME_preprocess_html(&$variables) {
  $variables['head_title']['title'] = 'title here';

Logic can be added for each page:

$path = $current_path = \Drupal::service('path.current')->getPath();
if ($path === '...')

Then, in html.html.twig:

<title>{{ head_title.title }}</title>

instead of

<title>{{ head_title|safe_join(' | ') }}</title>

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.