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.

Themes & Styling

How to use image from page folder into theme folder

Solved by Paul Massendari View solution

Started by Usk70 9 years ago · 3 replies · 853 views
9 years ago

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)

9 years ago

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 }}" />

9 years ago

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-

9 years ago Solution

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

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 196 2 months ago
Themes & Styling · by Ian, 2 months ago
3 92 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 454 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 47 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 126 3 months ago