I recently spoke at hugoconf2022 and I am trying to make my template more useful.
The repository is GitHub - reillymedia/hugoconf-perfect-lighthouse-scores: Perfect Lighthouse Scores 2022
It is open source and free for anyone to download.
I am trying to pass an image url from a template layout into a partial and have hugo images process the required images src set images. The only way i have been able to make this work so far is to have the actual image in the img/img.html (image processing partial) like this. This however only processes one image. I would like to be able to pass the variables using dict and produce the srcset images for each image.
Below is what I currently have.
{{/* get file that matches the filename as specified as src=“” in shortcode */}}
{{ $src := resources.Get “images/banner.webp” }}
{{/* set image sizes, these are hardcoded for now, x dictates that images are resized to this width */}}
{{ $tinyw := default “500x webp” }}
{{ $smallw := default “883x webp” }}
{{ $mediumw := default “1200x webp” }}
{{ $largew := default “1500x webp” }}
{{/* resize the src image to the given sizes */}}
{{ .Scratch.Set “tiny” ($src.Resize $tinyw) }}
{{ .Scratch.Set “small” ($src.Resize $smallw) }}
{{ .Scratch.Set “medium” ($src.Resize $mediumw) }}
{{ .Scratch.Set “large” ($src.Resize $largew) }}
{{/* add the processed images to the scratch */}}
{{ $tiny := .Scratch.Get “tiny” }}
{{ $small := .Scratch.Get “small” }}
{{ $medium := .Scratch.Get “medium” }}
{{ $large := .Scratch.Get “large” }}
{{/* only use images smaller than or equal to the src (original) image size, as Hugo will upscale small images /}}
{{/ set the sizes attribute to (min-width: 35em) 1200px, 100vw unless overridden in shortcode */}}
The code is
Perfect Lighthouse Scores with Hugo
The world's fastest static site generator.
Fix Your Lighthouse ScoresI want to pass img partial from the coverbanner.html using dict and have it work so that all the srcsets will work and it will be easy to pass something like {{ partial “img/img.html” dict ( “url” “images/banner.jpg” “alt” “Perfect Scores” }}
!–Cover Banner–>
Perfect Lighthouse Scores with Hugo
The world's fastest static site generator.
Fix Your Lighthouse ScoresThe code in the repository works as is in the repository but I am not able to use dict for each image passed in every partial. Any help would be appreciated.