Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Displaying image in extra folder specified in yaml frontmatter

Started by Otto Nurm 8 years ago · 2 replies · 831 views
8 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
8 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.%>

8 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 Jerry Hunt, 4 days ago
2 80 9 hours ago
General · by pamtbaau, 14 hours ago
1 51 13 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago