Error calling index: value is nil; should be of type string in theme/_default/li.html

Started building site
ERROR: 2016/11/08 12:07:44 template.go:131: template: theme/_default/li.html:27:33: executing “theme/_default/li.html” at <index .Site.Params.a…>: error calling index: value is nil; should be of type string in theme/_default/li.html
ERROR: 2016/11/08 12:07:45 template.go:131: template: theme/_default/li.html:27:33: executing “theme/_default/li.html” at <index .Site.Params.a…>: error calling index: value is nil; should be of type string in theme/_default/li.html

Here are the contents of li.html

{{ $baseurl := .Site.BaseURL }}
<div class="pure-g">
<article class="shadow post {{ .Section }} homepage-post pure-u most-recent">

    <header class="post-header">
        <h2 class="post-title"><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
    </header>
<a class ="article-list-link pure-u" href="{{.RelPermalink}}">
                    <img class="pure-img article-img" style="" src = "
                        {{ if isset .Params "og_image" }}
                        {{ .Params.og_image }}
                        {{else }}
                        {{ .Site.Params.og_image}}
                        {{ end }}
                    " alt="" />
                    </a>
                    <div class="article-list-text">
                        {{ if .Description }}
                            {{ substr .Description 0 180  }} ...
                            {{ else }}
                        {{if .IsPage}}
                            {{ .Summary }}
                            {{ end }}
                        {{ end }}
                    </div>

                    {{$author := index .Site.Params.authors .Params.author }}
                    <div class="pure-u post-meta ">By:
                        <a class="" href="{{$baseurl}}authors/{{ $author.name | urlize }}/">{{$author.name}}
                        </a>
                        Tags:
                        {{ range $index, $tag := .Params.tags }}
                        <a class="pure-u" href="{{$baseurl}}tags/{{ $tag | urlize }}/">#{{ $tag }}</a>,
                        {{ end }}
                        <time class="post-date pure-u" datetime="{{ .Date.Format "2006-01-02T15:04:05Z07:00" | safeHTML }}">{{ .Date.Format "2 Jan 2006" }}</time>


                        <div class="pure-g readmore" style="">
                            <a class="read-more btn pure-u" href="{{.RelPermalink}}">Read More</a>
                        </div>


</article>
</div>

I can’t figure out why the author bit is returning nil. Please advise. I am misunderstanding something.

I believe the index needs to a number; as in, think of the position of an index in an array. You might have better luck (don’t have time to test) with the following:

{{$thisAuthor := .Params.author }}
{{$author := range where .Site.Params.authors "==" $thisAuthor }}

But some of this is conjecture since I’m not sure how your params are set up in your config file.

Also, check out the default function since this might help reduce your if/else statements (just a thought). Cheers.

1 Like

config.toml

[params.authors]
[params.authors.jasmine]
    name = ""

    bio = "Jasmine's passion for writing is fueled by nature and the magic of every day life. Peace, love & truth are the messages that she has to share with others. To see more of her original work, please check out her Instagram."

    location = ""
    website = ""
    email = ""
    irc = ""
    steemit = ""
    twitter=  ""
    image = ""
    instagram = ""

author.html does not give error. but if I use the same line {{$author := index .Site.Params.authors (.Params.author)}}
in my other template file, it gives error. executing “theme/_default/li.html” at <index .Site.Params.a…>: error calling index: value is nil; should be of type string in theme/_default/li.html


{{$author := index .Site.Params.authors (.Params.author)}}
{{$authorname := or $author.name  }}
{{$authorbio := or $author.bio  }}
{{$authorlocation := or $author.location  }}
{{$authorwebsite := or $author.website .Site.Params.authorwebsite }}
{{$authoremail := or $author.email  }}
{{$authorirc := or $author.irc  }}
{{$authorsteemit := or $author.steemit  }}
{{$authortwitter := or $author.twitter  }}
{{$authorimage := or $author.image  }}
{{$authorinstagram := or $author.instagram  }}

<section class="author">
    <div class="author-info">
        <img class="author-thumb" src="{{$authorimage}}" />
        <h4><a href="{{.Site.BaseURL}}">{{$authorname}}</a></h4><span class="authorname-sub">Psychonaut</span>
        {{if $authorbio}}
        <p>{{$authorbio}}</p>
        {{else}}
        <p>Read <a href="{{.Site.BaseURL}}">more posts</a> by this author.</p>
        {{end}}
        <div class="author-meta">
            {{with $authorlocation}}<span class="author-location icon-location">{{.}}</span>{{end}}
            {{with $authorwebsite}}<span class="author-link icon-link"><a href="{{.}}">{{.}}</a></span>{{end}}
            {{with $authoremail}}<span class="author-location icon-location"><a href="mailto:{{.}}?Subject=Hello%20{{$authorname}}" target="_top">{{.}}</a></span>{{end}}
            {{with $authorirc}}<span class="author-location icon-location">{{.}}</span>{{end}}
            {{with $authorsteemit}}<span class="author-link icon-link"><a href="{{.}}">{{.}}</a></span>{{end}}
            {{with $authortwitter}}<span class="author-link icon-link"><a href="https://twitter.com/@{{.}}">Twitter: {{.}}</a></span>{{end}}
            {{with $authorinstagram}}<span class="author-link icon-link"><a href="https://www.instagram.com/{{.}}">Instagram: {{.}}</a></span>{{end}}
        </div>
    </div>

<div class="fb-twitter-share-buttons" style="text-align:center;">
    <div class="fb-like" data-href="{{ .Permalink }}" data-layout="button_count" data-action="like" data-size="small" data-show-faces="true"></div>
    <div class="fb-save" data-uri="{{ .Permalink }}" data-size="small"></div>
    <div class="fb-send" data-href="{{ .Permalink }}"></div>
    <div class="fb-share-button" data-href="{{ .Permalink }}" data-layout="button_count" data-size="small" data-mobile-iframe="true"><a class="fb-xfbml-parse-ignore" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u={{ .Permalink }}&amp;src=sdkpreparse">Share</a></div>


{{ if $authortwitter}}
    <a href="https://twitter.com/{{$author.twitter}}" class="twitter-follow-button" data-show-count="false">Follow {{$author.name}}</a>
    <a href="https://twitter.com/intent/tweet?screen_name=HashBorgir" class="twitter-mention-button" data-show-count="false">Tweet to {{$author.name}}</a>
</div>


</section>
{{end}}

This gives error, this is inside of recent_articles.html. Same line as author.html {{$author := index .Site.Params.authors (.Params.author)}} but author.html does not give error.

this file gives error. Why is the same code giving error in one place, and not in another?


            {{$author := index .Site.Params.authors (.Params.author)}}
            <div class="pure-u post-meta ">By:
                <a class="" href="{{$baseurl}}authors/{{ $author.name | urlize }}/">{{$author.name}}
                </a>
                Tags:
                {{ range $index, $tag := .Params.tags }}
                <a class="pure-u" href="{{$baseurl}}tags/{{ $tag | urlize }}/">#{{ $tag }}</a>,
                {{ end }}
                <time class="post-date pure-u" datetime="{{ .Date.Format "2006-01-02T15:04:05Z07:00" | safeHTML }}">{{ .Date.Format "2 Jan 2006" }}</time>


                <div class="pure-g readmore">
                    <a class="pure-u read-more btn" href="{{.RelPermalink}}">Read More</a>
                </div>

            </div>

This is the funny bit. If you go to any article on https://stoned.io you’ll see the author.html partial file rendering fine w/o errors.

if I use the same code in /partials/recent_articles.html I get this error.

1 Like

.Params.author is proably not set in “the other place”.

this would come from the article front matter, right?

+++
author = "jasmine"
authors = "jasmine-miller"
categories = ["Authors"]
date = "2016-11-03T23:55:33-06:00"
description = "This poem and photo were inspired by a combination of Cannabis and meditation."
google_news_keywords = [""]
og_audio = ""
og_image = "https://i.imgur.com/tF1VFsY.jpg"
og_image_url = "https://i.imgur.com/tF1VFsY.jpg"
og_video = ""
tags = ["Poetry"]
title = "Solitude And Grace - An Original Poem By Jasmine Miller"
+++

It’s set.

{{$author := index .Site.Params.authors (.Params.author)}}

If I use this code in /partials/author.html, I get no errors.

If I use this code in /partials/recent_articles.html, I get this error.

The .Params.author from article front matter is specified. The config.toml .Site.Params.authors is also specified.

I’m still confused.

Just to qualify, you are using both these partials in page and not node/list types layouts, yes? If you’re using the recent... partial in a node, it doesn’t have front matter to pull from. Again, just conjecture.

I see. when author.html is rendered in single.html (non-list?) layout, it’s fine.

When recent_articles.html is rendered (it’s rendered inside li.html (list layout?) it’s giving this error.

I’m confused as to what you mean by what you said.

The weird thing is that I just get the error, but the page is rendered fine.

There are “pages” and then there “lists/nodes”. I can expand more in a bit when I’m done with work. Here:

https://gohugo.io/templates/terms#example-terms-html-files