Sort Issues with String Array

I don’t know if this is a bug or if I’m missing something obvious, but here we go:

I have a front matter item in each blog post called “blog/tags” with tags for the post; something like:

title: "Post Title"
blog/tags:
- "A first tag"
- "Something"
- "Maybe yet another tag"

If I debug using:

{{ printf "%#v" (.Param "blog/tags")}}

It outputs:

 []string{"A first tag", "Something", "Maybe yet another tag"}

If I run:

{{ printf "%#v" (.Param "blog/tags" | sort)}}

I get the error:

executing "main" at <sort>: error calling sort: sequence must be provided

But If I generate an identical string array using the following + sort:

{{ printf "%#v" (slice "A first tag" "Something" "Maybe yet another tag" | sort)}}`

it outputs the string array as sorted alphabetically:

 []string{"A first tag", "Maybe yet another tag", "Something"}`

Am I missing something obvious / fundamental here? How I can get sort to run that “blog/tags” front matter array without throwing an error?

One of the pages that uses this template does not have “blog/tags” set in front matter. You need to code defensively.

Instead of this:

{{ .Param "blog/tags" | sort }}

Do this:

{{ with .Param "blog/tags" }}
  {{ . | sort }}
{{ end }}
1 Like

Oh lord, thanks @jmooring — so obvious. :man_facepalming: Is there a mode with more detailed error output? I feel like half the time I’m debugging something in Hugo it’s from a lack of additional details.

error calling sort: sequence must be provided

It’s telling you that you haven’t provided a sequence to be sorted, which means the sequence you provided is nil.

I understand, but I suspect I would have immediately understood the issue if Hugo had flagged, for example, the .md file it was trying to render. The error as it stands is fairly generic: simply that it encountered the nil array on a specific template, which can be confounding if you’re piping lots of different data through that specific template?

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