Hugo: .Scratch.SetInMap doesn't work in ranges in certain cases

Created on 25 Jun 2018  ·  5Comments  ·  Source: gohugoio/hugo

I have a bizarre issue that has been driving me nuts for several days 😡, and I'm pretty convinced that either I'm doing something completely stupid, or this is a bug in Hugo or Go itself 🤷‍♂️

Here's an example where I try to set an item in a map from within a loop:

{{ .Scratch.SetInMap "greetings" "english" "Hello" }}
{{ .Scratch.SetInMap "greetings" "French" "Bonjour" }}

{{range $index, $page := .Site.RegularPages}}
  {{ .Scratch.SetInMap "greetings" "russian" "Privet" }}
{{end}}

Greetings: {{.Scratch.Get "greetings"}}

The result I get (notice, no 'Russian' in the map):

Greetings: map[english:Hello french:Bonjour]

To try to work around this, I decided maybe I should try using .Site.Pages instead of .Site.RegularPages.

{{ .Scratch.SetInMap "greetings" "english" "Hello" }}
{{ .Scratch.SetInMap "greetings" "french" "Bonjour" }}


{{range $index, $page := .Site.Pages}}
  {{ .Scratch.SetInMap "greetings" "russian" "Privet" }}
{{end}}

Greetings: {{.Scratch.Get "greetings"}}

Sure enough, it works perfectly:

Greetings: map[russian:Privet english:Hello french:Bonjour]

Ok... so now by any normal logic this must mean that that there are 0 regular pages which is the only reason why a loop wouldn't run. So I added an asterisk every time it loops so I can visualize how many times it's running:

{{range $index, $page := .Site.RegularPages}}
  <div>*</div>
  {{ .Scratch.SetInMap "greetings" "russian" "Privet" }}
{{end}}

And I get a row of asterisks indicating that the loop is in fact running:

*
*
*
*
*
*
*

Ok so at this point I'm completely baffled. The loop works perfectly fine, yet whenever I use .Site.RegularPages it completely breaks .Scratch.SetInMap, leaving me to think that this has to be a bug.

Thanks in advance, and thanks for the amazing project 👍

Version Info

Hugo Static Site Generator v0.42.1 darwin/amd64

All 5 comments

Btw the following would seem to be a perfectly obvious workaround (note the if statement filtering the Kind, but the fact that we're still using .Site.Pages for the iteration):

{{ .Scratch.SetInMap "greetings" "english" "Hello" }}
{{ .Scratch.SetInMap "greetings" "french" "Bonjour" }}

{{range $index, $page := .Site.Pages}}
  {{if (eq $page.Kind "page")}}
    {{ .Scratch.SetInMap "greetings" "russian" "Privet" }}
  {{end}}
{{end}}

Greetings: {{.Scratch.Get "greetings"}}

But this doesn't work either...

Greetings: map[english:Hello french:Bonjour]

I'm pretty sure I'm overcomplicating this and it's really just some stupid mistake. If it is, let me know, otherwise I can share the rest of my debug attempts to save you guys some time (saving $page.Kind to a variable etc)

Please use https://discourse.gohugo.io/ for questions/troubleshooting. Also see Hugo Documentation.

This isn't a question, it's a bug report... I assume legitimate bug reports belong on Github not the discourse forum, unless you guys have some other system in place.

Relevant section from the README:

If you believe you have found a defect in Hugo or its documentation, use the GitHub issue tracker to report the problem to the Hugo maintainers. If you're not sure if it's a bug or not, start by asking in the discussion forum. When reporting the issue, please provide the version of Hugo in use (hugo version).

Ask your question on the forum and I will show tell how this works. Your code has a logical flaw.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

crash-dive picture crash-dive  ·  3Comments

antifuchs picture antifuchs  ·  3Comments

geddski picture geddski  ·  3Comments

digitalcraftsman picture digitalcraftsman  ·  3Comments

moorereason picture moorereason  ·  3Comments