Hi,
I am slowly building toward the perfect image rendering that does not use javascript nor LCP, using assets/, something I never considered, so that I can access the totality of my Images folder without worrying about removing what I don’t use, since hugo will only included stuff refered to by a (Rel/)Permalink.
So I took inspiration mainly from Responsive and optimized images with Hugo | BryceWray.com
Because my wishes are bit different from what is proposed I modified the code, which is challenging since I don’t understand it entirely… well I do but could not reproduce it from scratch.
Now I have execute of template failed: template: _default/_markup/render-image.html:2:21: executing "_default/_markup/render-image.html" at <.Resource.GetMatch>: can't evaluate field Resource in type goldmark.imageLinkContext, so I put the code below and you’ll most likely see what I try to do, and what went wrong.
Yes I did see a few posts seemingly mentioning that error, but either didn’t understand them or thought they didn’t fit my case.
Thanks !
{{- $src := .Resource.GetMatch (printf "%s%s" .Destination) -}}
{{- $dataSzes := "(min-width: 1024px) 100vw, 50vw" -}}
{{- $actualImg := $src.Resize "640x" -}}
<figure>{{ with .Title }}
<figcaption>{{.|$.Page.RenderString}}</figcaption>{{ end }}
<picture>
<source
srcset="
{{- with $respSizes -}}
{{- range $i, $e := . -}}
{{- if ge $src.Width . -}}
{{- if $i }}, {{ end -}}{{- ($src.Resize (printf "%sx%s" ) ).RelPermalink }} {{ . }}w
{{- end -}}
{{- end -}}
{{- end -}}"
sizes="{{ $dataSzes }}"
/>
</picture>
</figure>
Ok, I put {{- $src := resources.Get (printf "%s%s" .Destination) -}} and in general resources.Get wherever there was .Resource.GetMatch
and now the error is:
execute of template failed:
template: _default/_markup/render-image.html:4:22:
executing
"_default/_markup/render-image.html" at
<$src.Resize>: nil pointer evaluating resource.Resource.Resize
Built in 215 ms```
Great.
And the code is simpler too.
But I get an error "Error: add site dependencies: load resources: loading templates: "/home/drm/WEBSITE/themes/hugo-book/layouts/_default/_markup/render-image.html:40:1": parse failed: template: _default/_markup/render-image.html:40: unexpected EOF
and it’s annoying af, because it gives no more information and I can’t seem to pin-point the syntax typo. Is there somewhere a code validator for go+html or somethin’ ?
/themes/hugo-book/layouts/_default/_markup/render-image.html:12:5":
execute of template failed:
template: _default/_markup/render-image.html:12:5: executing
"_default/_markup/render-image.html" at <.IsBlock>:
can't evaluate field IsBlock in type resource.Resource
Built in 269 ms
First, you said your images are in the assets folder, but you don’t have an assets folder in the base folder of your project. Second, you should create images and their paths based on the path in your markdown content i.e assets/images/space/andromeda-galaxy.jpg. So, do that.
Based on the answer by @bep, I modified your code a bit.
You still must check if resources.Get... and .Page.Resources.Get... returned something! The nil pointer error is related to a resource that couldn’t be found, probably because of a typo.