5

I have in a twig template an image url, like "public://path/to/image.png". I want to get "sites/default/files/path/to/image.png" to use it in the template.

I thought it should work with something like this:

{{ url ('public://path/to/image.png') }}

or

{{ file_uri_scheme ('public://path/to/image.png') }}

But all I get are errors. I know that the second one is a PHP function, but is there any chance to do it with TWIG?

2 Answers2

10

You can use the twig function file_url to get a relative path:

{{ file_url('public://path/to/image.png') }}

Checkout the documentation under https://www.drupal.org/node/2486991

It accepts a uri and creates a relative path to the file. At least in Drupal 8.1.

Philipp Michael
  • 954
  • 1
  • 11
  • 22
1

Retrieving a URL to a page from a "path" is deprecated in D8. https://www.drupal.org/node/2073811

EDIT:

nah im wrong and just missunderstood the spelling ;/

the correct answer is: they are working on a twig extension. i was searching for a way to get my image uri / urls and achieved it with {{ node.field_image[delta].entity.url }}

Adrian Kremer
  • 94
  • 1
  • 3
  • This throws an error for me now... `Twig_Sandbox_SecurityError: Calling "url" method on a "Drupal\file\Entity\File" object is not allowed in "themes/ib/templates/layout/html.html.twig" at line 35. in Drupal\Core\Template\TwigSandboxPolicy->checkMethodAllowed() (line 101 of core/lib/Drupal/Core/Template/TwigSandboxPolicy.php).` – Nick Dec 14 '15 at 11:57