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?
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.
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
{{ 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.