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.

Archive

Custom page blueprint. Help with twig template for image display

Started by Muut Archive 9 years ago · 6 replies · 1342 views
9 years ago

Hi,

I have been struggling to get a twig layout working when I assign a custom page blueprint. In my blueprint I have created a new tab with some custom fields. one of these custom fields is an image. I can get the text custom fields to display no problem but not the image. Does any one have a code snippet to show how this works?

9 years ago

How is it stored in your page header?

9 years ago

Should be something like {{ header.myimage|first.path }}

9 years ago

You can tell exactly how it's stored in the page by using {{ dump(page.header) }} in your twig template and looking at the results in the debug bar.
https://learn.getgrav.org/advanced/debugging

Once you have the exact way it's stored, then something like this should work to display it:

{{ page.media[page.header.custom_image].html() }}

I'm just learning this stuff myself, but this approach is working for me.

9 years ago

thanks for the input guys. I am a step closer. When I use:

<img src="{{ page.header.image|first.path }}" alt="{{page.title}}" />

I can see that the img gets the path to the file, but when i paste that link into a browser I get a 404

the blueprint property is, and the image definitely exists at that path, checked on ftp

header.image:
type: file
label: Image
destination: 'user/themes/mytheme/assets'

9 years ago

after a bit more debugging i could see from a working image (using the default media) I could see that the path had a leading slash /user.... where as my output was <img src="user...... if I manually add a slash in front <img src="/{{page.header.... it seems to work okay now.

I don't think thats the best idea of course, so it may be a setup somewhere I am missing

9 years ago

Try something like this:

TWIG
{{ page.media[page.header.image|first.path].html('My title', 'Some ALT text', 'myclass') }}

By passing your custom image into page.media you gain all the functionality of the media object. The .html() method takes care of getting the correct path to the image and adding the title attribute.

If you haven't yet, take some time and read through the documentation here: https://learn.getgrav.org/content/media. I've been solving many problems similar to yours by trying code snippets from the media documentation.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1297 9 years ago
Archive · by Muut Archive, 9 years ago
2 894 9 years ago
Archive · by Muut Archive, 9 years ago
2 4024 9 years ago
Archive · by Muut Archive, 9 years ago
1 2899 9 years ago
Archive · by Muut Archive, 9 years ago
3 1082 9 years ago