How to use image from page folder into theme folder

I would like to refernece an image from page folder inside a template (theme folder).

Is that possible? if so, what do you need to do to use an image in page folder from the template? (the idea is that the exact name of the image would be configured in a variable)

If you want to get an image located in a particular page folder using twig:
{% set my-page-image = page.find('/my/page/path').media['image-name'] %}

then you could output the image html like {{ my-page-image.html }} or <img src="{{ my-page-image.url }}" />

Thanks, what if I would like to find an image from the page that invoked the template? -i.e., in twig you can not reference the exact page path as it might be different each time-

you can just use page instead of page.find , exemple:
{{ page.media.images|first }} --> Display the first image of the page
{{ page.media[‘myimage.jpg’ ]}} --> Display myimage.jpg
{{ page.media[header.selectedimage] }} --> Display the image selected in header.selectedimage

1 Like