[Solved] Error using delimit with a Scratch array

I’m getting an error when using delimit with an array built by Scratch

I have a variable $byline that lists author pages from a book reviews. I loop over that variable to pull the author name from the head matter of those pages:

{{ range $i, $v := $byline }}
    {{ with $v.Params.byline }}{{ $.Scratch.SetInMap "names" . . }}{{ end }}
{{ end }}

Then I can output nice comma separated values using delimit:

{{ delimit ($.Scratch.GetSortedMapValues "names" ) "," ", and" }}

I was on v0.37 and this worked fine. I upgraded to v0.39 to hopefully address another issue (calling {{ $.Scratch.Delete "names" }} was throwing an error) and the delimit line started giving this error:

ERROR 2018/04/21 15:07:30 Error while rendering "home" in "": template: theme/index-content.html:31:23: executing "theme/index-content.html" at <delimit ($.Scratch.G...>: error calling delimit: can't iterate over <nil>

Writing: {{ $.Scratch.GetSortedMapValues "names" }} outputs an array of author names as expected, and {{ range $.Scratch.GetSortedMapValues "names" }}{{ . }} {{ end }} iterates over the array and lists the names individually as expected, so the data seems correct and present.

Any guesses?

It is not clear to me what you are trying to achieve with those two dots. Creating a map listing keys marching their values ?

- names
  - aurhor1: aurhor1
  - aurhor2: author2

I assume you need them ordered by name ? Have you tried using $i as key instead of the author name?

Mmm you may have found a bug here… do you have a repo you can share ?

In the mean time and if you can forgo with the ordering, I’d suggest using slices instead of map.

{{ .Scratch.Add "names" (slice .) }}

And use your Delimit on that.

To be honest, I’m a bit unclear (I’m still very slow at parsing Go syntax in my head), but this was an example from another entry on this board, and it solved the problem I was having. I think I took it to as “I need two arguments here, and it will be harmless to use current context for both of them”

The repository is public. Please pardon the mess. I’m porting my templates from a previous system, so the files are full of old commented-out code. It’s very, very hacky right now

Here’s the line with delimit commented out. Running it with this uncommented throws the error for me.

I tried using {{ .Scratch.Add "names" (slice .) }} to no avail, but I’ll play around with it and see if I can get that to work.

Ok I don’t get any build error with the following:

{{ range $i, $v := $byline }}
    {{ with $v.Params.byline }}{{ $.Scratch.Add "names" (slice .) }}{{ end }}
{{ end }}
{{/* TODO: do correct commas between names */}} 
{{ with ($.Scratch.Get "names") }}
    {{ delimit . "," ", and" }}
{{ end }}
{{ $.Scratch.Delete "names" }} 

I couldn’t find any pages where there would be more than one byline though…

1 Like

:raised_hands:t2: Thank you!

There are only one or two, but this is a pattern I’m going to be using throughout the site to list books, tags, and other lists. It was just the first instance I reached.

1 Like