Drupal - Get site name in custom template

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 }}

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.

Tags:

Theming

8