How to access frontmatter variable in shortcode

Within a shortcode I’m trying to range over an array defined in the frontmatter but get only error messages if I’m trying to access the fields.

Here’s a stripped down version of the frontmatter I’m using:

---
title: A demo post
gallery: [
    { thumbnail: thumb1.jpg, "img1.jpg" },
    { thumbnail: thumb2.jpg, "img2.jpg" },
    { thumbnail: thumb3.jpg, "img3.jpg" }
]
---

Inside the shortcode a loop should range over gallery and create a list with thumbnails:

<ul>
{{ range $.Params.gallery }}
    <li><img src="{{ .thumbnail }}"></li>
{{ end }}
</ul>

But the console only shows error messages. Even trying to access the title of the post via {{ .Title }} or {{ $.Params.title }} doesn’t seem to work. With this simpler example I get the following error:

ERR: template: theme/shortcodes/gallery.html:3:8: executing “theme/shortcodes/gallery.html” at <$.Params.title>: can’t evaluate field title in type interface {}

1 Like

Try

{{ range $.Page.Params.gallery }}
3 Likes

Thanks a lot! this should be better documented. I’ve been looking for this for the last 2 hours.

Pull Request for improvements in to the documentation is always welcome.