Dynamically accessing front matter within shortcode

I’ trying to dynamically access front matter info from within a shortcode:

Front matter:

- src: image1.jpg
- src: image2.jpg
- src: image9.jpg
- src: image3.jpg
- src: image4.jpg

The shortcode should be something like

{{< gallery id="2" >}}

How can I dynamically get the front matter info from within a shortcode?

Trying to get a range working with a string obviously doesn’t work, but I can’t figure out how to do it right …

{{ $index := $.Get "id" }}
{{ $gallery := printf ".Page.Params.gallery-%s" $index }}

{{ range $gallery }}
{{ end }}

Given error is

range can’t iterate over .Page.Params.gallery

Which is totally reasonable as range can’t iterate over a string - but how can I build the name of the collection so range can work with it?


{{ with .Get "id" }}
  {{ range index $.Page.Params (printf "gallery-%s" .) }}
    {{ range $k, $v := . }}
      {{ $k }}: {{ $v }}<br>
    {{ end }}
  {{ end }}
{{ else }}
  {{ errorf "The %s shortcode requires an 'id' parameter. See " .Name .Position }}
{{ end }}

Thanks, seems “index” does the trick! :smiley:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.