1

I would like to include an image from custom_theme/images folder where custom_theme is a custom theme created following the grav documentation.

The twig template is in partials and this is the excerpt:

<!-- Logo -->
<a class="navbar-brand" href="./">
    <img class="logo logo-dark" alt="" src="/images/logo.png">
    <img class="logo logo-light" alt="" src="/images/logo-light.png">
</a>
<!-- #end Logo -->

What shall be used in src to refer to this image file: /user/themes/custom_theme/images/logo.png

M.E.
  • 4,955
  • 4
  • 49
  • 128

1 Answers1

4

Have you taken a look at the Grav documentation? I typed "image" to the search field and found this:

Twig Filters & Functions

Url

Will create a URL and convert any PHP URL streams into a valid HTML resources. A default value can be passed in in case the URL cannot be resolved.

url('theme://images/logo.png')|default('http://www.placehold.it/150x100/f4f4f4')/user/themes/learn2/images/logo.png

So you should probably have:

<!-- Logo -->
<a class="navbar-brand" href="./">
    <img class="logo logo-dark" alt="" src="{{ url('theme://images/logo.png') }}">
    <img class="logo logo-light" alt="" src="{{ url('theme://images/logo-light.png') }}">
</a>
<!-- #end Logo -->

If you want, you can use the basic default Twig filter to provide a fallback URL if the image can't be found. If you don't use the default filter and the image can't be found, the url function will return nothing (or actually null, but it will be converted into an empty string), as can be seen in the source code of the url function.

Matias Kinnunen
  • 7,828
  • 3
  • 35
  • 46