I understand the metadata loop as used in the Antimatter theme, but when I try to pull through a single metadata field it doesn’t work?
<meta name="description" content="{{ page.metadata.description }}">
{{ site.metadata.description }}
works fine. Can anyone explain why this is the case, and how I can do this?
You have set in this particular page’s frontmatter
metadata:
description: 'Your page description goes here'
Right?
Yeah, I have it set for every page.
I should have noted that not only does {{ page.metadata.description }}
not work, it breaks the site, throwing the following error:
‘An exception has been thrown during the rendering of a template (“Array to string conversion”)’
Yep, most definitely you should have.
Grav tells you rather preciselly what hurts it… I’m afraid that we’d need some more of your Twig code to debug this.
As the API Reference states:
metadata(array $var=null) : array an Array of metadata values for the page
Function to merge page metadata tags and build an array of Metadata objects that can then be rendered in the page.
You were trying to render an array as a string, it couldn’t happen, sorry.
There is a nice reference implementation of „how to use page.metadata()
on the Antimatter templates
Right, I understand that, but my question is why does {{ site.metadata.description }}
work? Is that not an array too?
Furthermore, is there no way to grab just that one field without looping through all the metadata?
Yes, it is quite possible, I believe that site.metadata.description
comes directly from config/site.yaml
, not from any magical function designed to throw all the metadata combined together
Yeah, there is, you have to access array’s item that has key ‘name’ equal to ‘description’ I believe.
That makes sense.
After playing around a bit I discovered that you can grab it outside of a loop with {{ page.header.metadata.description }}
. Problem solved.