Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Displaying image in extra folder specified in yaml frontmatter

Started by Otto Nurm 9 years ago · 2 replies · 1002 views
9 years ago

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:

TXT
![](/images/myimage.jpg)

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

yaml frontmatter:

TXT
header_image: /images/myimage.jpg

twig:

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
9 years ago

Hi,

try something like that:

TWIG
{% 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.%>

9 years ago

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 {% %}

Suggested topics

Topic Participants Replies Views Activity
General · by jeremycherfas, 1 week ago
4 181 2 days ago
General · by lynbor, 6 days ago
2 122 3 days ago
General · by Old Man Umby, 1 week ago
1 100 1 week ago
General · by Sebastian, 1 week ago
1 124 1 week ago
General · by Andy Miller, 3 weeks ago
0 248 3 weeks ago