Getting a strange index: value is nil error

Content:

---
title: Detroit Pistons
nickname: "pistons"
type: team-stats
---

Template:

{{ $data := index .Site.Data.nba.stats.teams .Params.nickname }}

Error:

Error: Error building site: failed to render pages: render of "page" failed: "/layouts/team-stats/single.html:15:16": execute of template failed: template: team-stats/single.html:15:16: executing "main" at <index .Site.Data.nba.stats.teams .Params.nickname>: error calling index: value is nil; should be of type string

Using this earlier in the file works just fine:

{{ if isset .Params "nickname" }}
{{ partial (printf "symbols/nba/_%s.svg" .Params.nickname) . }}
{{ end }}

And printing {{ .Params.nickname }} works fine as well. Why is this index not able to use find this parameter?

Using hugo v0.84.2+extended darwin/arm64 BuildDate=unknown, by the way.

I don’t know what your problem is, but I suspect you can work around it:

{{ if .Params "nickname" }}
{{ partial (printf "symbols/nba/_%s.svg" .Params.nickname) . }}
{{ end }}

I never use isset, but I suspect that it returns true for any value, including nil.

{{ if .Params.nickname }} works, with {{ if .Params "nickname" }} I get the error

executing "body" at <.Params>: wrong number of args for Params: want 0 got 1`

Yes, .Params is a map not a method.

You can do:

{{ if index .Params "nickname" }}

I still don’t get what’s happening here. I’ve simplified the layouts/team-stats/single.html file a bit to focus on what’s not working:

{{ partial (printf "symbols/nba/_%s.svg" .Params.nickname) }}

{{ $data := index .Site.Data.nba.stats.teams .Params.nickname }}

The content file I have at content/nba/team-stats is this:

---
title: Golden State Warriors
nickname: warriors
type: team-stats
---

And I get this error:

Start building sites … 
hugo v0.84.3+extended darwin/arm64 BuildDate=unknown
Total in 129 ms
Error: Error building site: failed to render pages: render of "page" failed: "/layouts/team-stats/single.html:2:3": execute of template failed: template: team-stats/single.html:2:3: executing "team-stats/single.html" at <partial (printf "symbols/nba/_%s.svg" .Params.nickname)>: error calling partial: partial "symbols/nba/_%!s(<nil>).svg" not found

If I remove the printf line, I get this error for the second call to .Params.nickname:

Start building sites … 
hugo v0.84.3+extended darwin/arm64 BuildDate=unknown
Total in 105 ms
Error: Error building site: failed to render pages: render of "page" failed: "/layouts/team-stats/single.html:2:12": execute of template failed: template: team-stats/single.html:2:12: executing "team-stats/single.html" at <index .Site.Data.nba.stats.teams .Params.nickname>: error calling index: value is nil; should be of type string

What is happening?

Have you defined “nickname” in the front matter of every page that calls this partial?

If not, do this:

{{ with .Params.nickname }}
  {{ partial (printf "symbols/nba/_%s.svg" .) }}
  {{ $data := index site.Data.nba.stats.teams . }}
{{ end }}
1 Like

There it is. Thank you! I knew I was missing something “simple” like this. Appreciate the help!

A post was split to a new topic: Question about template lookup order

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