How do I get the length of a front matter list via a shortcode?

For example:


    creators: ['Eric', 'John']


{{ $num_creators := len .Page.Params.item.creators }}

Results in
failed to process shortcode: “M:\repos\REMOVED\layouts\shortcodes\focusbox.html:41:28”: execute of template failed at <len .Page.Params.item.creators>: error calling len: reflect: call of reflect.Value.Type on zero Value

This in the shortcode:
{{ printf "%#v" .Page.Params.item.creators }}

Outputs this on the page:
[]interface {}{"Eric", "John"}

So clearly the front matter data is being accessed correctly.

It means that you have some pages without this front matter, you need to be more defensive in your template, e.g.:

{{ $num_creators := 0 }}
{{ with .Page.Params.item.creators }}
{{ $num_creators = len . }}
{{ end }}
1 Like

Ah, I see. Thank you for your help!

1 Like

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