Range offset variable undefined in 0.48+

Copied “as is” from the bug report that wasn’t considered a bug.

I believe I’ve found a problem with the usage of range as described in Example #3 of Templating Introduction to access to the current offset in an iteration after updating to Hugo 0.48.

I’ll provide all information plus the testing repository for search engine indexing sake:


title: "Some File"
url: /some-file
 - name: "Trail #1"
   url: "/url-for-trail-1"
 - name: "Trail #2"
   url: "/url-for-trail-2"
 - name: "Trail #3"
   url: "/url-for-trail-3"

Duis ut minim consectetur ex eiusmod quis elit velit et veniam deserunt qui elit pariatur duis eu elit est mollit deserunt ad eu dolor nisi deserunt.


{{ if (.Params.breadcrumbs) "!=" nil }}

  {{- $.Scratch.Set "breadcrumbs" slice -}}

  {{- $.Scratch.Add "breadcrumbs" (slice (dict "url" ("/" | absLangURL) "name" "Home" "position" 1 )) -}}

  {{ range $index, $element := (.Params.breadcrumbs) }}
    {{- $.Scratch.Add "breadcrumbs" (slice (dict "url" .url "name" .name "position" (add $index 2))) -}}
  {{ end }}

  {{- $length := len ($.Scratch.Get "breadcrumbs") -}}

  {{- range $index, $.Scratch.Get "breadcrumbs" -}}

    {{ if eq ( (int .position) ) $length }}
  <span class="breadcrumb-item active">{{ .name }}</span>
    {{- else -}}
  <a class="breadcrumb-item" href="{{ .url | absURL }}" >{{ .name }} > </a>
    {{- end -}}

  {{ end }}

{{ end }}


In Hugo 0.47 this compiles and the Breadcrumb Trail appears as expected:

Home > Trail #​1 > Trail #​2 > Trail #​3

In Hugo 0.48 and 0.49, however, it yields an error saying $index is not defined and shuts down the server:

Building sites . ERROR 2018/09/26 10:42:25 Error while rendering “page” in “”: template: page\single.html:27:44: executing “main” at <“breadcrumbs”>: undefined variable: $index
Total in 9 ms
Error: Error building site: logged 1 error(s)

I’ve searched about but I didn’t find anything mentioning that something changed. Did something really changed and the docs haven’t been updated?

As you see, I read the manual, I followed all the procedures (code, repository, log…) but it still not a bug and I’ve been kicked out Github to here.

So much for trying variable reassignments… :frowning:

This does not look like the example you link to.

Hmm… Indeed, it’s slightly different, just by inject a randomly named variable before the Scratch, even if that variable isn’t even used in the inner block, it worked in both versions, but in any case it might have something wrong with 0.47 and lower then because tat shouldn’t be working until now.

{{- range $index, $foo := $.Scratch.Get "breadcrumbs" -}}

Now tell me why don’t write these two lines in the issue itself? Why closing it with some predefined message if you could’ve solved everything there? Look at the time wasted creating a whole new topic, in a different place. Follow the K.I.S.S. principle, man.

You have no idea how many hours of work that principle (“questions belong on the forum”) has saved me. It is called delegation. I usually use an automated reply on GitHub for questions, takes me 2 seconds to resolve an issue. Here, other people may chime in with a solution. On GitHub, we would probably been flooded by unresolved issues if not for this simple rule: Questions belong on the forum. Does that make sense?


Maybe we should create a GitHub issue template on the main Hugo repo with a notice about support questions belonging to the forum and that GitHub issues are reserved only for bug reports and feature requests.

This isn’t a problem, and if it was, the real problem is that in most of these cases, the question looks like a real bug to the issue starter. So an issue template would just be added noise, I suspect.

1 Like

Why do you add 2 to the index here? I see you create a new slice with the home URL to start with. So that gets you the first element. But aren’t slices zero-based? In that case there’s missing an element from creating the first (index 0) to the first added in the loop (starting at index 2).

Does this miss a piece of code? You can range ($.Scratch.Get "breadcrumbs") and you can range $i, $v := ($.Scratch.Get "breadcrumbs"), but I thought you cannot range $i ($.Scratch.Get "breadcrumbs").