8

I want to print the site name in a custom template (page--front.html.twig)

{{ site_name }} does not work.

If I use {{ page['#title'] }} I only get "Welcome to [site_name]".

I'm looking for a way to only print the site_name. Do anyone know how to achieve this?

Thanks!

11

Add this to the YOUR_THEME.theme file:

function YOUR_THEME_preprocess_page(&$variables) {
  $variables['site_name'] = \Drupal::config('system.site')->get('name');
}

And then this in your page--front.html.twig template:

{{ site_name }}
6

Things have changed some from Drupal 7 to Drupal 8. One of the things that have changed, is that it's a really good idea to use blocks instead of fetching and rendering this stuff yourself. The main reason is caching.

What you probably want to do, is to place the site branding block somewhere on the page (can display site name, logo and/or slogan). A lot of the stuff that was hardcoded in themes, are now blocks.

There was a big effort in doing this, as it makes the caching and huge performance gains possible, which was achieved in Drupal 8.

If you really want to do it the Drupal 7 way, @Aram's answer shows you how to do it with a preprocess hook.

  • Very interesting. Thanks! I found a way to implement the block in a *.twig template by using following snippet: {% include 'themes/bootstrap/templates/block--system-branding-block.html.twig' %} . Is this okay or would you recommend another approach? – Stefan Feb 23 '16 at 13:00
  • @Phil I would recommend that you make a region in your theme, and configure and place the block. If needed to can create a custom template to alter the markup generated, most of the times that wont be necessary. – googletorp Feb 24 '16 at 9:32
1

Yes in D8 twig template files you can not write code to fetch data.

If you need any data then you need to set the variables in preprocess function & use that.

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.