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.

Archive

Image metadata file

Started by Muut Archive 11 years ago · 5 replies · 2005 views
11 years ago

Looking to provide a caption for image files in a post, I tried using the documented an_image.jpg.meta.yaml. But having trouble accessing a field in the header or determining the exact structure of that met file. Docs are weak in this area so any direction welcome.

11 years ago

I admit they are a weak with that because the .yaml is not fully fleshed out and supported yet. However, there is an easier solution. Simply use the Markdown image format to provide the Alt text:

TXT
![Alt text](http://yoursite.com/image.jpg "Image Title")
11 years ago

Thanks but need to do this in a template with Twig. Guess I could name the file with the caption I want - is there a function that returns just the name of the media item or I guess I could just use php/twig.

11 years ago

Ok, just ran a quick test.

First I have a file called sample-image.jpg, so I created a file called sample-image.jpg.meta.yaml and in that file I put:

YAML
alt_text: My Alt Text
width: 900px
description: A sample description here.

Then i dumped out the object in my Twig with:

TWIG
{{ dump(page.media['sample-image.jpg']) }}

Then in my browser (with debugger enabled of course), I can see the object in the Messages panel:

Media | Demo2 2014-12-29 11-54-52

Scrolling down to the bottom I can see:

JS
#items: array:15 [
    "type" => "image"
    "thumb" => "/Users/rhuk/workspace/grav-demo-sampler/user/pages/07.media/sample-im age.jpg"
    "mime" => "image/jpeg"
    "image" => array:1 [
      "filters" => array:1 [
        "default" => array:1 [
          0 => "enableProgressive"
        ]
      ]
    ]
    "name" => "sample-image.jpg"
    "filename" => "sample-image.jpg"
    "basename" => "sample-image"
    "extension" => "jpg"
    "path" => "/Users/rhuk/workspace/grav-demo-sampler/user/pages/07.media"
    "modified" => 1414346083
    "width" => "900px"
    "height" => 768
    "size" => 74
    "alt_text" => "My Alt Text"
    "description" => "A sample description here."
  ]

So then I can see the values are showing up in the object as expected and I can access them directly such as:

TWIG
{{ page.media['sample-image.jpg'].alt_text }}

and it simply displays:

TXT
My Alt Text

Works fine! I love it when a plan comes together!

11 years ago

Perfect, Andy, works a treat – playing about with the blog skeleton for my eldest's site and nearly done. The skeleton makes life very easy and helps to familiarise with Grav for a bigger project. Will send you a link when my hosting updates me to PHP 5.5 (still rockin' 5.3!)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1318 9 years ago
Archive · by Muut Archive, 9 years ago
2 915 9 years ago
Archive · by Muut Archive, 9 years ago
2 4044 9 years ago
Archive · by Muut Archive, 9 years ago
1 2920 9 years ago
Archive · by Muut Archive, 9 years ago
3 1103 9 years ago