Get path to theme assets in Twig template I have a question about getting a path to an image in a Twig template. The image is not assigned to a field or something. Just a static image which is stored in "MYTHEME/image/icon/my-icon.png".

In Drupal 7 I get the path in my node.template with the following code:

&lt;img src="&lt;?php print base_path() . path_to_theme(); ?&gt;/image/icons"&gt;/my-icon.png


How does it work with in Drupal 8? I tried to pass a variable in template_preprocess_node().

MYTHEME.theme:

$variables['images_path'] = \Drupal::theme()-&gt;getActiveTheme()-&gt;getPath() . '/image/';


Twig template:

&lt;img src="{{ images_path  ~ 'icons/' ~ 'my-icon.png' }}"&gt;


It doesn't work. There is no PHP error, but the path is wrongly said to be http://localhost/node/themes/template/image/icons/my-icon.png.
you can use {{ base_path ~ directory }} which will fix the absolute problem, no need to do any preprocessing, both of these variables are included by core.

For example

&lt;img src="{{ base_path ~ directory }}/images/logo.png" alt="My Logo" /&gt;


PS. the ~ helper in twig is concatenate.

Edit: at least in the page*.html.twig templates base_path variable is included, possibly you will need to do preprocessing for other templates, you can easily check with {{ dump() }} if the variables are present

// File: THEMENAME.theme in your theme's root directory
function THEMENAME_preprocess(&amp;$variables, $hook)
{
    $variables['base_path'] = base_path();
}