[SOLVED] Error calling index: value is nil; should be of type string

Template changed “/home/stoned/websites/websites/psychedelicsdaily.com-hugo/themes/hugo-universal-theme/layouts/partials/css.html”: CREATE
ERROR 2018/02/09 10:18:46 Error while rendering “taxonomyTerm” in “”: template: theme/_default/list.html:49:45: executing “theme/_default/list.html” at <index .Site.Params.a…>: error calling index: value is nil; should be of type string

Is the error I get. This is the line in question: {{$author := index .Site.Params.authors (.Params.author) }}

This only started to happen after I upgraded my Hugo. I forgot what the older version was.

config.toml has

author=authors as taxonomy

Article front matter has

+++
author="hash"
authors="hash-borgir"
+++

and the config.toml file has

[params.authors.hash]

{{$author := index .Site.Params.authors (.Params.author)}} works in single.html (single page template), but when in list.html, it gives an error.

Could it be that single.html can read the article front matter, in which .Params.author is defined as front matter author=“hash” cannot?

I’m guessing list.html template pages can’t read article front matter of pages? I’ve tried something like:

{{ range .Data.Pages }}
     {{$author := index .Site.Params.authors (.Params.author)}}
{{ end }}

and that still gives me the error.

That code giving the error: <index .Site.Params.a...>: error calling index: value is nil; should be of type string basically expects (.Params.author) to be a thing, which it is not, hence the nil error.

.Site.Params.authors defines an array of authors. Article front matterauthor="" bit (.Params.author) is the actual author in the .Site.Params.authors array.

How can I make list template pages get access to .Params.author?

Note: This only gives an error in Hugo logs, but actually continues to function… for some odd reason.

If you go to https://www.psychedelicsdaily.com/authors/hash-borgir or https://www.psychedelicsdaily.com/tags/dmt you’ll see the list pages, and how each author is properly functioning, but I still get this error, and I’d like to not have this error anymore.

Let me know what else I can help you to do understand my problem. Please advise, guys.

Sincerely,

Hash B.

It would help to know from which version to which you upgraded.

But in any case…

You are using .Site.Params.…, but setting the parameters in the page. If you want to access the page params, use just .Params.

.Site.Params would be the ones you define in [Params] in your config.toml.

Update: Nevermind, I see that you have that part set correctly…

Then may be you don’t set the same front-matter in the section _index.md files too, and that’s the problem?

On another note, I am a fan of with, so that you don’t try to access stuff that is not defined… how about something like:

{{ with .Params.author }}
    {{ $author := index $.Site.Params.authors {{ . }} }}
    /* more logic */
{{ end }}

I thought about that _index.html bit, but if I do

_index.md

+++
author=“hash”
+++

I will be able to put “hash” (.Params.author) into .Site.Params.authors so I can retrieve the values for that author, but then each _index.md will only show one author.

.Site.Params.authors is multiple authors:

In config.toml

[params.authors.hash]
[params.authors.augustine]
[params.authors.guest]

A list page shows 2 pages for example.

Page 1 is by author.hash
Page 2 is by author.augustine

How do I make list.html (list templates) go into each page that it lists, into that page’s front matter, and access that page’s .Params.author?

That’s what I need to do.

I need access to every single list item in the list template’s front matter. Each article could have a diff author. I need access to that.

Does this make sense?

EDIT: For example

{{$author := index .Site.Params.authors (.Params.author AS DEFINED IN THE ARTICLE'S FRONTMATTER) }}

How would this be?

.Site.Params.authors(.Page.Parmams.author) ? Something like that anyway.

I see that you updated the snippet in the original post. Now it shows:

So that would then just loop through regular pages (not sections).

If even then you get that error, then one of the pages doesn’t have the author defined in front-matter.

Hmm. Actually it should be .RegularPages, right, so _index.md doesn’t get listed?

All the content pages have author defined afaict.

You are probably right! I haven’t yet played much with those. Just try it out :slight_smile:


Update

Nope, looks like .Data.Pages lists only regular pages.

Tried by putting this in my index.html:

{{ range .Data.Pages }}
     {{ partial "debugprint.html" (slice .Kind .Title) }}<br>
{{ end }}

debugprint.html partial


Update 2

You can even extend that to:

{{ range .Data.Pages }}
    {{ if .Params.author }}
        <b>Author is {{ .Params.author }} for {{ partial "debugprint.html" (slice .Kind .Title) }}</b><br>
    {{ else }}
        No author for {{ partial "debugprint.html" (slice .Kind .Title) }}<br>
    {{ end }}
{{ end }}

That gives me this on my site:

image

1 Like

In _default/list.html

I changed my paginator to .Paginate .Site.RegularPages

I think .Data.Pages was pulling the _index.md pages in my content categories which don’t all always have author="" bit defined.

{{ $paginator := .Paginate .Site.RegularPages }}

I don’t see any more errors in the log. Page seems to work fine. But now ever taxonomy shows all the site pages.

So, is there something like.Data.RegularPages (I can’t use .Site.Pages or Site.RegularPages because I need to stay within the paginator context .Data).

I still struggle with hugo template syntax.

Is it possible to change this into whatever would give me:

Change

From: {{ $paginator := .Paginate .Data.Pages }}

To: {{ $paginator := .Paginate .Data.Pages (WHERE .Data.Pages DOES NOT INCLUDE _index.md)}} ?

I can’t figure out the “where” clause in this go syntax. Please advise.

EDIT: I came up with:

{{ $paginator := .Paginate where .Data.Pages "!=" "_index.md" }}

ERROR 2018/02/09 11:18:59 Error while rendering “taxonomy” in “tags/microdosing/”: template: theme/_default/list.html:34:27: executing “theme/_default/list.html” at : wrong number of args for where: want at least 2 got 0

{{ $paginator := .Paginate where .Data.Pages .File.BaseFileName "!=" "_index.md" }}

Template changed “/home/stoned/websites/websites/psychedelicsdaily.com-hugo/themes/hugo-universal-theme/layouts/_default/list.html”: CREATE
ERROR 2018/02/09 11:20:05 Error while rendering “taxonomy” in “tags/microdosing/”: template: theme/_default/list.html:34:27: executing “theme/_default/list.html” at : wrong number of args for where: want at least 2 got 0

Dang nabbit. I can’t figure this one out alone.

{{ $paginator := .Paginate .Data.Pages }}
{{ range $i, $e := where ($paginator.Pages) ".Params.hidden" "!=" true }}
<section class="post shadow box">
<div class="row">
    <div class="col-md-4">
        <div class="image shadow">
            <a href="{{ .Permalink }}">
                {{ if .Params.banner }}
                <img src="{{ .Site.BaseURL}}img/placeholder.png" data-src="{{ .Site.BaseURL}}{{ .Params.banner }}" class="img-responsive" alt="">
                {{ else }}
                <img src="{{ .Site.BaseURL}}img/placeholder.png" class="img-responsive" alt="">
                {{ end }}
            </a>
        </div>
    </div>

    <div class="col-md-8 post-meta">
        <h2><a href="{{ .Permalink }}">{{ .Title }}</a></h2>
        <div class="clearfix">
            <p class="author-category">
                {{ if isset .Params "author" }}
                {{$author := index .Site.Params.authors (.Params.author) }}
                <a class="" href="{{$baseurl}}authors/{{ .Params.authors | urlize }}/"><i class="fa fa-user" aria-hidden="true"></i>{{$author.display_name}}</a>
                {{ end }}

            {{ range $index, $tag := .Params.tags }}
            <a class="" href="{{$baseurl}}tags/{{ $tag | urlize }}/"><i class="fa fa-hashtag" aria-hidden="true"></i>{{ $tag }}</a>,
            {{ end }}

                {{ if isset .Params "categories" }}
                {{ if gt (len .Params.categories) 0 }}
                <a href="{{ $.Site.BaseURL }}categories/{{ index .Params.categories 0 | urlize | lower }}"><i class="fa fa-folder" aria-hidden="true"></i>{{ index .Params.categories 0 }}</a>
                {{ end }}
                {{ end }}

            </p>
            <p class="date-comments">
                <a href="{{ .Permalink }}"><i class="fa fa-clock-o"></i> {{ .Date.Format .Site.Params.date_format }}</a>
            </p>
        </div>

        <p class="read-more"><a href="{{ .Permalink }}" class="btn btn-template-main shadow"><i class="fa fa-list-alt" aria-hidden="true"></i> {{ i18n "continueReading" }}</a>
        </p>
    </div>
</div>
</section>
{{ end }}

I moved the author bits under the if sanity check. Now error goes away and everything seemingly still functions.

Just checking out the debug partial! Superb work. Thanks for taking the time to help me look.

That’s very odd. I cannot recreate that… .Data.Pages pulls in only regular pages for me (no section content pages). May be @bep can chime in for clarification.

Cool! Though, once you get comfortable with the with syntax, you’ll stop using if and isset where possible :smile:


On a slightly tangential note, @regis has written this excellent blog post on dotI am mentioning this post because you will come across this concept if you start using with.


Thanks. It’s a growing piece of work… As I come across debugging types that aren’t automatically detected as a map or slice, I update it.

Just to answer this question that went unresolved:

The fix is to provide “string” arguments (except the first argument) to the where function.

So if you want to list everything except for the pages whose .Kind is "section", you’d do the below (yes, using "Kind" instead of .Kind, and "ne" instead of ne):

{{ $paginator := .Paginate (where .Site.Pages "Kind" "ne" "section") }}
1 Like