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.