Accessing metadata of images outside a pages folder does not work

Hi all,

I have the following structure and located files:

user/pages/01.home/firstpicture.jpg
user/pages/01.home/firstpicture.jpg.meta.yaml
user/pages/images/secondpicture.jpg
user/pages/images/secondpicture.jpg.meta.yaml

within the yaml-files, the metadata is set via
alt_text: my information

Now, this works within user/pages/01.home/default.de.md:
{{ page.media['firstpicture.jpg'].meta.alt_text|e }}

This does not work:
{{ pages.find('/images').media['secondpicture.jpg'].meta.alt_text|e }}

What am I doing wrong? I try to adopt the howto of this post: https://discourse.getgrav.org/t/media-in-twig-template/522/4

Thank you for any advice,

Markus

Does your /images folder actually represent a page (does it have a .md file)?

To be clearer, try

dump( pages.find('/images') )
dump( pages.find('/images').media['secondpicture.jpg'] )

to see if you are in the right place.

Hi phi,

thanks for your hints, but sadly it does not get me further.

Does your /images folder actually represent a page (does it have a .md file)?

No, it doesn’t. I’d like to use the folder to place my pictures all together in one folder instead of a decentralized placement all over different folders. That makes it much more easier to keep an eye on them and benefits structured assets.

I hope I did it right to change your code to

{{ dump( pages.find('/images') ) }}
{{ dump( pages.find('/images').media['secondpicture.jpg'] ) }}

Sorry if that is kind of noob, but I justed started to get into GRAV.

Well, nothing happend, even when I put

{{ dump(page.header) }}
{{ dump('i am just a string') }}

into the default.de.md-file to avoid issues with wrong paths. The complete code is

---
title: Testpage
published: true
process:
    markdown: true
    twig: true
---

#Testpage
{{ dump(page.header) }}
{{ dump('i am just a string') }}
{{ dump( pages.find('/images') ) }}
{{ dump( pages.find('/images').media['secondpicture.jpg'] ) }}
Lorem ipsum.

Well, the headline and Lorem ipsum is shown in frontend but nothing in between, even no markup - so I still don’t have ideas to make it work.

Some more information to dig the reason for my problem: I set up the standard GRAV installation with admin plugin (1.6.28) on localhost using xampp with php 7.3, just followed the instructions and checked the possibilities of learn.getgrav.org till chapter 2.

The debugger in frontend just says:

  • Grav v1.6.28 | info
  • Environment Name: localhost | info
  • Cache: [false] Setting: [auto] Driver: [file] | info
  • Routed to page /test (type: default) | info
  • {#251 +“title”: “Testseite” +“published”: true +“process”: array:2 [ “markdown” => true … | debug
  • i am just a string | debug
  • null
  • null

Okay - “dump” seems not to be meant to show anything in frontend, is it? Seems to me it is just meant to be used via debugger? So “null” means at least there is nothing found and there might be a path based issue?

Sorry for my long post.

Need some more help… :thinking:

Hi @Markus

Sorry if I confused you at first. I was intending to put the dumps somewhere in your twig template. But you figured out a nice work around and taught me something in the meantime :slight_smile:

So - the nulls in your output are as I suspected. There is no markdown in the path, so page.find will return null.

I use meta files, too, but only in pages. So I was curious.

The following should work:

{{ media['image://my_image.jpg'].meta.title }}

Let the media object find the image with the images:// prefix on the path and it should resolve the associated meta file.

Hooray!

Your suggestion made me have a look at https://learn.getgrav.org/16/content/media
and I altered it to
{{ media['user://pages/images/secondpicture.jpg'].meta.alt_text|e }}
after that, the in user/pages/images/secondpicture.jpg.meta.yaml contained information
alt_text: my super important image description
is shown in frontend!

Thanks a lot! :slight_smile:

Fantastic @Markus!

Glad it’s working and I learned some things too!

  • phi