I have the following code in layouts/_default/single.html
:
{{ with .Resources.GetMatch "header-image.jpg" }}
<img src="header-image.jpg" class="post-header-image" width="{{ .Width }}" height="{{ .Height }}">
{{- end }}
{{ with .Resources.GetMatch "header-image.png" }}
<img src="header-image.png" class="post-header-image" width="{{ .Width }}" height="{{ .Height }}">
{{- end }}
It displays jpg
image, and if it does not exist, it loads a png
file (there’s always one or the other).
When I attempt to extend this to include webp
:
{{ with .Resources.GetMatch "header-image.webp" }}
<img src="header-image.webp" class="post-header-image" width="{{ .Width }}" height="{{ .Height }}">
{{- end }}
{{ with .Resources.GetMatch "header-image.jpg" }}
<img src="header-image.jpg" class="post-header-image" width="{{ .Width }}" height="{{ .Height }}">
{{- end }}
{{ with .Resources.GetMatch "header-image.png" }}
<img src="header-image.png" class="post-header-image" width="{{ .Width }}" height="{{ .Height }}">
{{- end }}
I get an error that (WebP) resource is not an image:
Failed to render pages: render of "page" failed: "(...)/layouts/_default/single.html:23:69": execute of template failed: template: _default/single.html:23:69: executing "main" at <.Width>: error calling Width: *resources.genericResource is not an image
The error is caused by .Width
and .Height
. Without them Hugo does not error out and the webp
image is displayed correctly.
What do I need to do, in order to take advantage of .Width
and .Height
when working with webp
images?