1
votes

How can i get the base path of drupal installation in twig file?

For example i need to get http://localhost/drupal/.

I have tried

{{ base_path }}

But it's returning empty.

{{ directory }}

This is returning the theme path. but i need the base path to complete the url. Is there any way to do this?

2

2 Answers

2
votes

@Ditto P S To get base path on twig you declare a variable in .theme file inside a preprocess

enter image description here

andthen simply use 'base_path_success' variable in twig as

{{ base_path_success }}

You can declare variable inside any of the preprocess like

function ttnd_preprocess_node(&$variables) {}

OR

function ttnd_preprocess_block(&$variables) {}

Here ttnd is themename.

For more information you can use the below link

https://drupal.stackexchange.com/questions/205289/base-path-drupal-8

0
votes

You can get the hostname, "localhost/drupal/", directly from the getHost() request:

$host = \Drupal::request()->getHost();

In some cases you might want to get the schema as well, fx http://localhost/drupal/:

$host = \Drupal::request()->getSchemeAndHttpHost();

Source : https://drupal.stackexchange.com/a/202811/