Ok, so total noob question but I’m trying to understand how you load images in Grav. For example, lets say I have a blog post and for each post I want the background image (hero) to be different for each blog post.
From what I gather it looks like I need to drop these images in the same folder as the markdown content… but how I do load that into my template?
Here is a line from my template: style="background-image: url({{ hero_image.url }});"
I’m unsure what “hero_image” is referencing. I thought it was referencing the image when you are looking at in the admin view, which is: ![hero_image](build_kit_hero.png)
I’m getting nothing to show on the page, so something is not working. I’ve been combing through the documentation and forum and cannot find something that spells this out.
hero_image is just variable and you should find something like {% set hero_image = page.media.images|first %} in your template. In this example the hero_image is first image in page media collection.
This is a good idea and would love to make this happen. I’ve got a blueprint in my blueprints folder that has what you suggested, but I’m not seeing anything yet in the admin. Should it display as another tab next to Advanced or something?
I think what I am missing is the Front Matter that sits at the top of the .md page. What should I have there?
Hey Andy, thanks for the reply and a link to your example! I’m trying to make it so that on the page I’m building it has a hero image for the background (via CSS and not inline) that a user could add from the admin panel.
My biggest problem is I’m still so new to Grav and just having a hard time wrapping my head around how it all ties together!
Mmh well that depends, you can create another tab or just set a new field in one the default tabs. Anyway I’m going to explain you the easy way:
Let’s suppose you have your blog-post twig template like ‘item.html.twig’, you should create a blueprint just for the blog-post following this folders structure:
themes > mytheme > blueprints > item.yaml
Inside the item.yaml file we set a new fieldset with a filepicker for your image. All this is going to be placed in the Content Tab:
Your explanation definitely helps, thank you. My admin view is still not displaying anything for me. I cleared the cache on the dashboard to help but no luck. Here’s what I’ve got:
Ok, the code you suggested worked great, it was a tabbing issue on my end. Guess I need to pay close attention to that. Thanks for the responses on all of this, very helpful!
Not trying to make life hard, just still trying to learn Grav.
I went back through what you wrote versus what I had and saw where I screwed up. So right now I do have this: {% set style = ’ style=“background-image: url(’’ ~ page.media.images[page.header.hero_image].url() ~ ‘’)”’ %}
and this: <div {{ style }}>
But I’m getting this error:
Twig_Error_Syntax
Unexpected token “name” of value “style” (“end of statement block” expected).
Hi dan!
I will try to explain it with my bad English, sorry about that
At first - {% block %} ... {% endblock %} uses for partials/base.html.twig which is supplemented by the current template. These blocks are used in it - you can see references of this blocks into the partials/base.html.twig. Therefore, {% endblock%} is no needed for a {% set%} statement.
At second, it looks like your line {% set style = ’ style=“background-image: url(’’ ~ page.media.images[page.header.hero_image].url() ~ ‘’)”’ %} uses a wrong quotes because you just make copy-paste of @fjrfac answer ( copy-paste - the worst enemy of a developer) You need to replace wrong quotes - use only ' and "
Sorry for the delay. Yeah underr is right.
It seems that when I copied the code, Discourse deleted some backslash (I don’t know why) so I’m uploading a screenshot with the code.
Hey guys, so I was able to get it working all the way through. Most of the reason I couldn’t get things working because I didn’t have enough understanding on how TWIG works and some of the naming conventions within GRAV. I found these 2 videos last night which really helped:
MD
—
title: TJ
hero_image: testimage.jpg
—
Lorem ipsum
Really, thanks for helping me through this. I feel like I’ve started (just a little!) to understand how all of this pieces together. One follow up question to all of this…
Using my YAML file is it possible to reorder the fields in the CMS? I plan to have a decent amount of fields in there and would like the order of them to make sense to the user.