Displaying image in extra folder specified in yaml frontmatter

Hi,

I have several images stored in a seperate image folder user/images (at the same level as user/pages).

I can display images from that folder with markdown:

![](/images/myimage.jpg)

But I have a twig that reads the image to display from the yaml frontmatter.
Here is an example:

yaml frontmatter:

header_image: /images/myimage.jpg

twig:

<% if page.header.header_image %>
  {{ page.media[page.header.header_image] }}
<% endif %>

However, this only works if the image specified in the yaml frontmatter is in the same directory as the page. When it is an absolute (or relative) path pointing to an image in the images directory (as in the example above) no image is displayed. There is not even any HTML generated for displaying the image.

What am I doing wrong?

1 Like

Hi,

try something like that:

{% set p = page.find('/images',true) %}
{% set cover = p.header.header_image %}

{% if cover %}
    {{ p.media[cover] }}
{% endif %}

Remember to use {% if etc.%} instead of <% if etc.%>

Hello iusvar,

thanks for the idea, but this does not work. If I understand your code correctly, it would always try to retrieve the image from the /images folder. However I would like to have a flexible way to load it either from an arbitrary folder (either as absolute or relative link).

However, the above mentioned example doesn’t get any image loaded

BTW: The <% %> were a mistake in my post. They are actually {% %}