Hi All,
I’m trying to optimise images within a template which pulls a range of images in an array. Similar to what was asked here. However, I am unsure how to translate the solution to put it into the template I am using. I have read the documentation and searched lots of YouTube videos but still can’t get it to work.
What I am trying to achieve is to convert the images to a .webp
format with fallbacks for older browsers.
The array is in a partials html as:
<div class="screenshots-slider">
{{ $Section := .images }}
{{ range $Section }}
<div class="screenshots-slider-item">
<img src={{ .url }} alt="" />
</div>
{{ end }}
</div>
This utilised a .yml
file in the /data
directory which contains:
---
enable: true
title: This is a title
description: This is my description
images:
- url: images/screenshots/slider1.png
- url: images/screenshots/slider2.png
- url: images/screenshots/slider3.png
- url: images/screenshots/slider5.png
- url: images/screenshots/slider6.png
- url: images/screenshots/slider7.png
- url: images/screenshots/slider8.png
These .png
files are currently in the static/images/screenshots
directory.
This is what I’ve tried which causes errors:
{{ $Section := .images }}
{{- $banner := resources.Get "{{ .url }}" }}
{{- $jpeg := $banner.Resize (printf "%dx%d jpeg" $banner.Width $banner.Height) }}
{{- $webp := $banner.Resize (printf "%dx%d webp photo" $banner.Width $banner.Height) }}
{{ range $Section }}
<div class="screenshots-slider-item">
<picture>
<source srcset="{{ $webp.RelPermalink }}" type="image/webp">
<img src="{{ $jpeg.RelPermalink }}" loading="lazy" width="{{ $banner.Width }}" height="{{ $banner.Height }}" alt="" />
</picture>
</div>
{{ end }}
When trying to build the hugo website with my changes the terminal gives the following error:
\partials\screenshots.html:26:32": execute of template failed: template: partials/screenshots.html:26:32: executing "partials/screenshots.html" at <$banner.Resize>: nil pointer evaluating resource.Resource.Resize
I appreciate any guidance you can offer.
Many Thanks
*edited to provide more information on the directories of the files
** added terminal output error