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.

Content & Markdown

Accessing metadata of images outside a pages folder does not work

Solved by Markus View solution

Started by Markus 6 years ago · 6 replies · 739 views
6 years ago

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: /forum/archive/media-in-twig-template-t4744

Thank you for any advice,

Markus

6 years ago

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

6 years ago

To be clearer, try

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

to see if you are in the right place.

6 years ago

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

TWIG
{{ 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

TWIG
{{ 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

TWIG
---
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... 🤔

last edited 11/01/20 by Markus
6 years ago

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 🙂

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:

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

6 years ago Solution

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! :)

Suggested topics

Topic Participants Replies Views Activity
Content & Markdown · by Jochen, 8 months ago
6 99 8 months ago
Content & Markdown · by Ton Haarmans, 1 year ago
10 186 1 year ago
Content & Markdown · by Jan L'Am, 1 year ago
4 148 1 year ago
Content & Markdown · by Leonardo, 1 year ago
3 62 1 year ago
Content & Markdown · by belthasar, 1 year ago
4 257 1 year ago