Passing Variable through a Range

I have a range that looks like this:

{{ range (seq 0 1) }}
  {{ .Site.Data.exampledata.examplearray ( . | int ) "example key" }}
{{ end }}

when I run this, I get an error:

execute of template failed at <.Site.Data.rfl_data.teams>: can’t evaluate field Site in type int

/home/runner/RFL/site/layouts/_default/home.html:5:16:
3.    <ul>
4.      {{ range (seq 0 1) }}
5.        {{ .Site.Data.rfl_data.teams ( . | int ) "name" }}`
6.      {{ end }}
7.    </ul>

https://gohugo.io/templates/introduction/#the-dot

When you’re in a range or with block, the context (the dot) is the pipeline that you passed.

To get to the context passed into the template, preface with a $.

{{ $.Site.Whatever }}

or you can use the global site function:

{{ site.Whatever }}

If you’re new to Hugo, understanding how context works should be at the top of your reading list.

2 Likes

What if I had multiple ranges, like this:

{{ range (seq 0 1) }}
  {{ . }}
  {{ range (seq 0 3) }}
    {{ . }}
    {{ "some way to access previous range variables" }}
  {{ end }}
{{ end }}

Store the outer range dot in a variable. That will be accessible in the inner range.

1 Like

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