Can't index .Site.Author with Page.Params.author

Doing {{ index .Site.Author .Params.author }} causes an error:

execute of template failed at <index site.Author .Params.author>: error calling index: value is nil; should be of type string 

I checked, and no value is nil. See below. Indexing with an identical string literal does work.

config.yaml:

author:
  george_bluth:
    display_name: George Bluth
  michael_bluth:
    display_name: Michael Bluth

Front matter:

+++
author = "michael_bluth"
foo = "michael_bluth"

Template:

<p>.Site.Author: {{ .Site.Author }}</p>
<p>printf "%T" .Site.Author: {{ printf "%T" .Site.Author }}</p>
<p>.Params.author: {{ .Params.author }}</p>
<p>printf "%T" .Params.author: {{ printf "%T" .Params.author }}</p>
<p>eq .Params.author "michael_bluth": {{ eq .Params.author "michael_bluth" }}</p>
<p>index .Site.Author "michael_bluth": {{ index .Site.Author "michael_bluth" }}</p>

Output:

.Site.Author: map[george_bluth:map[display_name:George Bluth] michael_bluth:map[display_name:Michael Bluth]]

printf "%T" .Site.Author: map[string]interface {}

.Params.author: michael_bluth

printf "%T" .Params.author: string

eq .Params.author "michael_bluth": true

index .Site.Author "michael_bluth": map[display_name:Michael Bluth]

Note that {{ index .Site.Author .Params.foo }} also results in the same error.

Sorry in advance if I’m missing something obvious, but after a half hour of debugging, I just can’t see it.

Full verbose error output

ERROR 2022/12/19 15:11:54 render of "taxonomy" failed: "/Users/Will/Developer/paige/layouts/_default/taxonomy.html:2:3": execute of template failed: template: _default/taxonomy.html:2:3: executing "main" at <partial "paige-article.html" .>: error calling partial: "/Users/Will/Developer/paige/layouts/partials/paige-article.html:4:7": execute of template failed: template: partials/paige-article.html:4:7: executing "partials/paige-article.html" at <partial "paige-authors.html" .>: error calling partial: "/Users/Will/Developer/paige/layouts/partials/paige-authors.html:4:3": execute of template failed: template: partials/paige-authors.html:4:3: executing "partials/paige-authors.html" at <index .Site.Author .Params.author>: error calling index: value is nil; should be of type string
Error: Error building site: failed to render pages: render of "page" failed: "/Users/Will/Developer/paige/layouts/_default/paige-search.html:2:3": execute of template failed: template: _default/paige-search.html:2:3: executing "main" at <partial "paige-article.html" .>: error calling partial: "/Users/Will/Developer/paige/layouts/partials/paige-article.html:4:7": execute of template failed: template: partials/paige-article.html:4:7: executing "partials/paige-article.html" at <partial "paige-authors.html" .>: error calling partial: "/Users/Will/Developer/paige/layouts/partials/paige-authors.html:4:3": execute of template failed: template: partials/paige-authors.html:4:3: executing "partials/paige-authors.html" at <index .Site.Author .Params.author>: error calling index: value is nil; should be of type string

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.108.0+extended darwin/amd64 BuildDate=unknown

Does this issue reproduce with the latest release?

Yes

I will look at this in a bit, but with this in mind…

https://github.com/gohugoio/hugoDocs/commit/2c0125b5290494d49334606c451446ebd9df3c21

… I wouldn’t use .Site.Author.

Instead, I would use (in order of preference):

  1. Hugo’s taxonomy system
  2. Something in site configuration under the params key (e.g., [params.authors])
  3. A data file (e.g., site.Data.authors.jdoe)
1 Like

One or more of the content pages rendered by this template does not define author in front matter.

You need to code defensively:

{{ with .Params.author }}
  {{ index site.Author . "display_name" }}
{{ end }}

@jmooring Thanks, that must have been it!

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