I’m having trouble understanding the object returned from resource lookup and why I can’t get a permalink in certain contexts.
Background:
Trying to use resources for image lookup in order to auto generate blog summary with title image. One method for single pages (1) works. For the list layout (2), though, I can return an image resource (it finds it), but can’t seem to evaluate Permalink or any attributes for that matter when assigned to a variable.
I am wondering if, because one of the pages does NOT have a title image, and hence returns nil, calling a lookup on a variable containing nil pops the error and I need a logic step to prevent lookups on empty sets.
Example Page: **All pages use same archetype, though ONE of them purposefully does NOT have a title_img
---
title: "Testpost"
date: 2019-06-21T23:36:58-06:00
tags: []
draft: true
resources:
- src: "mango1.jpg"
name: "title_img"
params:
ref: "101"
alt: "Beautiful Woman"
---
TestPost Content Text Below Frontmatter
<!-- {{< figure src="mango1.jpg" title="babe for sure" >}} -->
Trying to get the picture by resource…
(1) Working resource call for single.html:
{{ define "main" }}
BLOG SINGLE HTML
<h1>{{ .Title }}</h1>
<div class="container">
<main id="main">
{{ range .Resources.Match "title_img" }}
<img src="{{ .Permalink }}" alt="{{ .Params.alt }}">
{{ end }}
<p>
{{ .Content }}
</p>
</main>
</div>
{{end}}
(2) List Layout - Yields execute fail: executing “main” at <$title_img.Permalink>
: nil pointer evaluating resource.Resource.Permalink.
{{ define "main" }}
BLOG LIST HTML
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{.Date.Format "2006-01-02"}} | {{.Title}}</a>
{{ $title_img := .Resources.GetMatch "title_img" }}
<h4>{{ $title_img.Permalink }}</h4>
</li>
{{ end }}
</ul>
{{ end }}
Thanks!!