Error: Can't evaluate field RelPermalink in type string

This example reproduces the error with hugo server and hugo server -D (but my main project reproduces with hugo server -D). Files of concern: layouts/_default/home.search.json and content/books/test. I kindly request pointers on why the error is showing and how to resolve it.

$ hugo env
hugo v0.138.0-ad82998d54b3f9f8c2741b67356813b55b3134b9 linux/amd64 BuildDate=2024-11-06T11:22:34Z VendorInfo=gohugoio
GOOS="linux"
GOARCH="amd64"
GOVERSION="go1.23.2"

– needed to update put the (not needed) {{$page = . }} code to the wrong location and simplified the code
this code:

{{- $image := ""}}
{{- with $img }}
  {{- $image = .Process (printf "fill %dx%d webp TopLeft" (div .Width 12) (div .Height 7.85 | int) ) }}
{{- end }}

will leave $image set to an empty string if there’s no resource found above
and theres no $image.RelPermalink for a string

Solution:

I would suggest to code defensive reporting errors or warnings - will ease up debugging

This Partial
{{- $list := slice -}}

{{- range site.RegularPages -}}

  {{- $img := "" }}
  {{- with .Resources.GetMatch "cover.*" }}
    {{- $img = . }}
  {{- end }}

  {{- $image := ""}}
  {{- with $img }}
    {{- $image = .Process (printf "fill %dx%d webp TopLeft" (div .Width 12) (div .Height 7.85 | int) ) }}
    {{- $image = .RelPermalink -}}
  {{ else }}
    {{- warnf "missing cover image for page %s" . -}}
  {{- end }}

  {{- $item := dict
    "title" .Title
    "summary" (.Summary | plainify)
    "image" $image
    "url" .RelPermalink -}}

  {{- $list = $list | append $item -}}
{{- end -}}

{{- $list | jsonify -}}

will produce:

hugo v0.138.0-ad82998d54b3f9f8c2741b67356813b55b3134b9+extended windows/amd64 BuildDate=2024-11-06T11:22:34Z VendorInfo=gohugoio

WARN  missing cover image for page .../content/sculptures/sculpture-20.md
# and many more

and set the image field in your dict to “” guess you will have to adjust that


btw - looks like there are no images found, and the test subfolder is not processed
dunno if .Process can fail, but if another with would be nice
using a {{ $page = . }} at the beginning of the range will ease up page specific log output even in nested with statements. so you could combine all the with used in a chain

2 Likes

Thanks a ton! Works with no errors.

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