Issue using index in single.html

Hello all!

I’m having an issue with my single.html. I’m trying to use a with index statement but it’s not working as expected.

single.html:

{{ define "main" }}
	<article class="post h-entry">
		<h1 class="post-title p-name" itemprop="name headline">
            <a href="{{ .Permalink }}">{{ .Title }}</a> {{ if .Draft }}(Draft){{ end }}
        </h1>
        <div class="image">
			<img src="{{ .Params.cover_image }}">
        </div>
		<div class="post-content e-content" itemprop="articleBody">
            {{ .Content | safeHTML }}
        </div>

		<p class="meta">
            Posted on <span class="postdate"><time class="dt-published">{{ .Site.Params.DateForm | default "Jan 2, 2006" | .Date.Format }}</time></span>
            {{ with index .Site.Data.authors .Params.author }}
		    by <span class="p-author h-card"><a href="{{ .link }}">{{ .name }}</a>
		    <img class="u-photo" style="display: none;" src="{{ .photo }}" />
		    </span>
            {{ end }}
        </p>
	</article>
{{ end }}

Problem code:

            {{ with index .Site.Data.authors .Params.author }}
		    by <span class="p-author h-card"><a href="{{ .link }}">{{ .name }}</a>
		    <img class="u-photo" style="display: none;" src="{{ .photo }}" />
		    </span>
            {{ end }}

When running this way I receive the error

execute of template failed at <index .Site.Data.authors .Params.author>: error calling index: value is nil; should be of type string render of "page" failed: value is nil; should be of type string  failed to render pages: value is nil; should be of type string 

If I replace .Params.author with a string like, "phil" then the code works as expected.

Post frontmatter:

---
title: "Xyz"
date: 2022-08-23T10:54:07-07:00
draft: false
slug: "xyz"
description: "Testing site"
author: "phil"
tags:
  - go
cover_image: https://example.org/imgs/sdl2-logo.png
---

So the question is how is .Params.author empty? I can print it to the page just before the with index.

The error is thrown because one or more of your content pages is missing the author front matter. You need to code defensively for this.

diff --git a/layouts/_default/single.html b/layouts/_default/single.html
index 54cf47b30..fcebe4e21 100644
--- a/layouts/_default/single.html
+++ b/layouts/_default/single.html
@@ -7,15 +7,17 @@ <h1 class="post-title p-name" itemprop="name headline">
                        <img src="{{ .Params.cover_image }}">
         </div>
                <div class="post-content e-content" itemprop="articleBody">
-            {{ .Content | safeHTML }}
+            {{ .Content }}
         </div>
 
                <p class="meta">
             Posted on <span class="postdate"><time class="dt-published">{{ .Site.Params.DateForm | default "Jan 2, 2006" | .Date.Format }}</time></span>
-            {{ with index .Site.Data.authors .Params.author }}
-                   by <span class="p-author h-card"><a href="{{ .link }}">{{ .name }}</a>
-                   <img class="u-photo" style="display: none;" src="{{ .photo }}" />
-                   </span>
+            {{ with .Params.author }}
+              {{ with index site.Data.authors . }}
+          by <span class="p-author h-card"><a href="{{ .link }}">{{ .name }}</a>
+          <img class="u-photo" style="display: none;" src="{{ .photo }}" />
+          </span>
+              {{ end }}
             {{ end }}
         </p>
        </article>

Hah. You were right one of my example posts was missing author. I’ve updated my code to account for that like you suggest. Thanks!!!

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