Using delimit with Params

Hi there! I’m new here. I started using Hugo a few weeks ago. I’ve never programmed in Go before but the documentation is really very good and I’ve been able to solve all my own problems so far. I’ve run into a bit of a weird issue, though, I thought I’d seek help on as I can’t find the exact answer on these forums.

In my frontmatter, I have a list within a map key within a list whose type is returning as []interface {}. This isn’t a problem as it works fine with range, but it does not work with delimit which is what I’m looking to use. However, when I create a list at the top level, it has type []string {}. I’m unsure if this is expected behaviour or I’m missing something or doing something else wrong.

Here is my code (the culprit is the roles key):

---
players:
  - name: Bob
    roles: [guitar bass vocals]
---
{{ range .Params.players }}
  {{ printf "%T" .roles }} {{/* This outputs `[]interface {}` */}}
{{ end }}

Compare this to:

---
test: [one two three]
---
{{ printf "%T" .Params.test }} {{/* This outputs `[]string {}` */}}

Any insight would be much appreciated!

I do have a work-around for this (mutating a $delim variable like I did way back in my PHP days) but it would be nice to be able to use delimit.

Thanks, y’all!

Your YAML is malformed. Should be this:

players:
  - name: Bob
    roles: [guitar, bass, vocals]

Hi, thanks for the response!

And yes, that’s what I get for not copy/pasting my code. I’m actually using the - syntax and it’s still an issue. It’s also still an issue if I add the commas with the [] syntax.

Here’s my actual frontmatter (with names changed, lol):

---
players:
  - name: Some Guy
    roles:
      - vocals
      - guitars
      - bass
      - drums
      - percussion
---

That’s still returning a []interface {} as is:

players:
  - name: Some Guy
    roles: [vocals, guitars, bass, drums, percussion]

Maybe I don’t understand the problem.

With this frontmatter:

---
title: Test
players:
  - name: Some Guy
    roles:
      - vocals
      - guitars
      - bass
      - drums
      - percussion
---

And this template code:

{{ range .Params.players }}
  Name: {{ .name }}<br>
  Roles: {{ delimit .roles ", " ", and " }}
{{ end }}

I get this:

image

Ah, I figured it out.

It’s because other content of the same type didn’t have the roles key. Due to my newness I wasn’t considering how all affected content re-renders. Wrapping in a with solves my problem.

Thanks so much for indulging!