Processing Twig in Page Frontmatter?

I’ve set the following in my site.yaml file:

frontmatter:
process_twig: true

And am trying to calculate the URL for an image for Twitter Card data:

metadata:
'twitter:card' : summary
'twitter:site' : @hibbittsdesign
'twitter:title' : CMPT-363 Week 2 (Sep 13 - 19)
'twitter:description' : Week 2 Materials for CMPT 363 User Interface Design at Simon Fraser University
'twitter:image': page.media.images|first.absolute_url

However, the above does not work and if I add brackets, like this:

'twitter:image': {{ page.media.images|first.absolute_url }}

I get a Twig error, any ideas on if/how this could be done?

Thanks very much,
Paul

So, I’ve discovered two things: one is that this processing happens before page processing so I cannot use page functions, however I also learned how to make this work otherwise!

You need to include quotes around the Twig code, so for example:

    'twitter:image':  '{{ base_url_absolute }}{{ page.find(''/headerimage'').media.images|first.url(true) }}'

Also notice that if you need to use quotes in the Twig line itself, you need to double quote them.

2 Likes