"len of untyped nil" on list I can successfully range over

I’m writing a new theme from scratch. I have a single index.html and a single partial. This is index.html in its entirety:

 {{ $paginate := .Paginate (where .Site.Pages "Type" "post") }}
  {{ range $paginate.Pages }}
    {{ partial "detail.html" . }}
  {{ end }}

And this is the detail.html partial:

 <div class="tags">
        {{ $total := len .Params.tags }}
        {{ range $i, $tag := .Params.tags }} // this works if I remove the line above
        <a href="{{ "/tags/" | relURL }}{{ $tag |urlize}}">{{ $tag }}&nbsp;</a>
        {{ end }}

I’m able to range over .Params.tags without issue, but calling len on .Params.tags throws:

 at <len .Params.tags>: error calling len: len of untyped nil

Content example:

title: "Test"
date: 2018-07-18:37:59-07:00
tags: ["test"]
draft: false


hugo env:

 Hugo Static Site Generator v0.42.2 linux/amd64 BuildDate: 2018-07-02T23:39:55+0200
1 Like

Untested wild guess: does the Param you’re testing for exist on all content? In other contexts “with” helps this.

The content is only that single example post, so can confirm it’s not from missing tag frontmatter. However {{ with .Params.tags }} produces a new error:

can't evaluate field Params in type []string

I’m getting the same issue when I try it as toml frontmatter as well:

title = "Test"
date = "2018-07-18:37:59-07:00"
tags = ["test"]
draft = false

For some reason it evaluates properly with range but not with or len

Super odd, if I add {{ printf "%#v" .Params.tags}}

I get []string{"test"} so it’s getting populated for sure

Also in the top example I use .Site.Pages which does include a top level “Posts” obj without tags, but using .Data.Pages doesn’t, and produces the same result.

Have you figured it out this problem?

I did not

I know that the original question is from ages ago, but have you tried .Page.Params.tags from within the partial?

This seems like a scoping issue.

I have exactly the same issue. I have this code:

{{ $pgs := .Param "componentOf" }}
{{ printf "Variable: %#v\n" $pgs }}

It generates either []string{} or []string{ "..." }, yet:

{{ $pgs := .Param "componentOf" }}
{{ printf "Variable: %#v\n" $pgs }}

{{ $pgsc := len $pgs }}
{{ printf "%+v\n" $pgsc }}


Rebuild failed:

Failed to render pages: render of "section" failed: "/Users/bsheward/site/themes/genesis/layouts/service/list.html:15:40": execute of template failed: template: service/list.html:15:40: executing "main" at <len $pgs>: error calling len: reflect: call of reflect.Value.Type on zero Value

13 {{ printf "Variable: %#v\n" $pgs }}
15 {{ $pgsc := len $pgs }}
16 {{ printf "%+v\n" $pgsc }}

Surely len should return 0 if it is passed an empty string array, right?

I encountered the same error. In my case, the tags were not defined for some posts, so it was failing to process those (even though the single file I was testing on had tags aplenty). Testing using ‘with’ first fixed it.