YAML map can't be indexed

Dear community,

I’ve been struggling on this for the past 3 hours.

My FrontMatter includes a YAML map:

  - title: nice-bedroom
    image: "/img/projects/nice-bedroom.jpg"
    thumbnail: "/img/projects/nice-bedroom.jpg"
  - title: nice-desk
    image: "/img/projects/nice-desk.jpg"
    thumbnail: "/img/projects/nice-desk.jpg"
  - title: nice-entrance
    image: "/img/projects/nice-entrance.jpg"
    thumbnail: "/img/projects/nice-entrance.jpg"

As you could have understood, I want to output a gallery. Given the following template code:

{{ range $image := .Params.gallery }}
  {{ reflect.IsMap $image }}
  {{ index $image "title" }}
{{ end }}

I get the following error: executing "main" at <index $image "title">: error calling index: cannot index slice/array with type string.

The weird thing it that the IsMap function return true. If I output $image, I get the following output:

map[image:/img/projects/nice-bedroom.jpg thumbnail:/img/projects/nice-bedroom.jpg title:nice-bedroom]

I’ve tried other ways of creating the YAML without any success. Any idea is welcome!

Thank you!

Hugo Static Site Generator v0.60.1-96066756 linux/amd64 BuildDate: 2019-11-29T14:57:23Z

Hi there,

I tried running your code and am unable to replicate your issue. Do you have a repo somewhere we can have a look at?

Perhaps you have a gallery item somewhere defined as a string instead of a map, like:

  - "lorem"

It’s always better to look at a problem after a good night.

I was transitioning from a gallery array to a map and forgot one content node.

So as you said @pointyfar, the code is working as expected. Thank you for giving me a clue where to look!

Have a wonderful weekend!