David Lohmeyer's Blog

Print your theme's logo path in Drupal 8 with Twig

I'm updating a Drupal 6 theme to Drupal 8.  One thing I'm doing is making the logo in my Twig template a Twig variable instead of hardcoding the path.  Here's how you do it.  This assumes a theme named 'acton', but you'll change that to your own theme's name.

In 'acton.theme', assuming your logo is 'logo.png' in your theme's root:

function acton_preprocess_page(&$variables) {
  $variables['logopath'] = '/' . drupal_get_path('theme','acton') . '/logo.png';
}

In your Twig template, do something like this:

<img class="img-responsive" src="{{ logopath }}" />

Done!

Related Posts