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.

Themes & Styling

Why does site.metadata.description work but page.metadata.description doesn't?

Solved by Joe Joiner View solution

Started by Joe Joiner 9 years ago · 7 replies · 1599 views
9 years ago

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?

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

9 years ago

You have set in this particular page's frontmatter

YAML
metadata:
    description: 'Your page description goes here'

Right?

9 years ago

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")'

9 years ago

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

last edited 11/22/17 by Makary
9 years ago

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?

9 years ago

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 ;)

@joejoinerr:
Furthermore, is there no way to grab just that one field without looping through all the metadata?

Yeah, there is, you have to access array's item that has key 'name' equal to 'description' I believe.

9 years ago Solution

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.

9 years ago

Yay! ;) Happy to hear

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 198 2 months ago
Themes & Styling · by Ian, 2 months ago
3 94 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 456 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 48 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 128 3 months ago