HUGO

Len function fails after hugo server restart

Hello,

I have partial, called on two page templates, which takes a parameter to set its heading depending on the page. It generates a list of people from the front matter on each page.

The code in each front matter gives a few people:
[[people]]
name = “x”
[[people]]
name = “y”
etc

The code to call the partial on each template:
{{ partial "people-list.html" (dict "page" . "peopleListHeading" "Heading text") }}

The code for the partial itself:
{{ .peopleListHeading }} ({{ len .page.Params.people }})

{{ range .page.Params.people }}
{{ .name}} etc
{{ end}}

I just want to show the number of people listed in each instance of the front matter on the relevant page.

Hugo server gave no errors when I originally saved the partial, but the length didn’t show. I added some random text to the file, resaved the partial and the length showed correctly in the brackets. However, stopping and restarting the server then fails to build and gives the following error:
error calling len: len of untyped nil

The people tables in each front matter do have several contents in them each. I’m not sure what the ‘untyped’ is referring to or why it thinks it is nil. Please could anyone help?

If you want to access page variables in a shortcode, you’ll need to prefix them with .Page (like .Page.Title for the page’s title). But that is not something we do in partial files; we just use .Title there.

Or, in your case, use .Params.people in your partial file instead of .Page.Params.people.

Thank you for such a quick response.

I had the .pages in there because of something on this comment here - Error Passing Data into Partials

which said that it needs .pages if you’ve passed in a variable via the partial call - have I confused partial with shortcode? I was just following along blindly rather than understanding the meaning behind it.

I’ve removed .pages but it still gives error calling len: len of untyped nil unfortunately!

I assume you have pages without … people. You need to do something ala:

{{ with .Page.Params.people }}({{ len .}}){{ end }}

Thank you very much - that appears to have worked.

Just to check I understand correctly - even if a partial is only called on 2 pages (which have the params needed for functions to work) if the rest of the site’s pages don’t have those params the partial needs to account for that otherwise it will fail to render?

No, that is not true. If all your pages that you use this partial on had “people” then your original code would have been fine. But that was obviously not the case.

1 Like

Yes it was being called on other pages. Thank you for your help.

1 Like