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.

General

How to add common conditional banner image for blog post headers

Started by gautam 8 years ago · 7 replies · 789 views
8 years ago

I have added the images folder inside my pages folder, now in my template that loads each indivisual blog post , i have the following code:

{% set blog_image = page.find('/images').media['indivisual-article-banner.jpg'] %}

this does't seem to work though as my blog image is not loaded at all.

How how do i retrieve my image saved in my images folder , also , i'd like to conditionally load this image I.E. check if the page header has a banner image set , and if not load the above generic header , so i guess i will need a or statement , i can check if the page has a header image by doing the following: page.header.header_image , but how do i pull up the generic header ?

EDIT::- i found this media object meathod, not ure how do i use it though !

last edited 03/09/18 by gautam
8 years ago

Hi,

Are you sure there is a md file in your /images folder?

Because the twig you posted seems correct.

If you want this image to be used as a "fallback" you can use the |default filter like this

TWIG
{{ page.header.header_image|default(blog_image) }}
👍 1
8 years ago

How do i set the blog_image to the image in my images folder ( name of default image is default_banner_image.jpg ).

The path of my images folder is user/pages/images.

8 years ago

The twig you posted:

TWIG
{% set blog_image = page.find('/images').media['indivisual-article-banner.jpg'] %}

Is correct and should work if you have a md file in /user/pages/images

👍 1
8 years ago

My folder structure is the following:

  • images
  • 01.home
  • 02.blog
    • my-first-blog-post
      • default.md
    • blog.md
    • someimage.jpg

What i want to do is access the image inside the images folder and add it as a banner image for default.md inside my-first-blog-post. How do i do this ? hope this makes sense , basically i want to store images for generic use in my images folder.

8 years ago

You sure you have an md file in /images folder?

Also, you can enable debugger and add {{ dump(page.find('/images).media }} and see what the output is in the debug bar.

👍 1
8 years ago

ok almost there , thanks for your patience in helping me out @paul .

So i have the following lines of code now:

TWIG
{% set generic_main_banner_image = page.parent.media.images|first %}
{% set blog_image = page.header.main_banner_image|default(generic_main_banner_image) %} 

I don't have main_banner_image set in any of my page headers , so the default should take over.

Since i want a spcecific image i try the following:

TXT
generic_main_banner_image = page.parent.media.images['i.jpg']

This does't seem to work though, but when i do the below:

TXT
generic_main_banner_image = page.parent.media.images|first

i do indeed get the image , why does the array syntax not work now ?

EDIT ::- on further debugging i found out that somehow the image array(page.parent.media.images) has only one image , when actually that folder has 2 image , when i output the following statement

TWIG
{{ page.parent.media.images|length }}  // Outputs 1... should be 2 actually !

I am a bit confused now :face_with_raised_eyebrow:😑😞

last edited 03/11/18 by gautam
8 years ago

Alright issue resolved , apparently i needed to add the following line of code to my blog.md file:

TXT
![Generic banner image](indivisual-article-banner.png)

without this the image i believe is not picked up by twig. thanks a ton for the help ! :) 😃

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 82 12 hours ago
General · by pamtbaau, 17 hours ago
1 55 16 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 348 5 days ago
General · by Duc , 5 days ago
3 43 5 days ago