Thanks for the pointers. I got it working after quite a bit of experimentation. Although I couldn’t get some of the more full-featured responsive image modules to work (I had trouble tracking down where the builds were failing), the code in those modules was helpful in getting me to a working version that suits my purposes which is multiple image sizes and formats using picture sources with a fallback to a low resolution image in the img tag for backwards compatibility.
In case it’s helpful for anyone else I’ll document my solution here. I welcome any pointers if the more experienced here see issues I’m creating for myself!
/layouts/partials/responsive-img.html:
{{ $img := .img }}
{{ $alt := .alt }}
{{ $class := .class }}
{{ $lazy := .lazy }}
{{ $caption := .title }}
{{- $respSizes := slice "960" "1920" -}}
{{- with $src := resources.Get ($img) -}}
{{- $divClass := "" -}}{{/* Init'g */}}
{{- $imgClass := "lazy" -}}
{{- $dataSzes := "(min-width: 1024px) 100vw, 50vw" -}}
{{- $actualImg := $src.Resize "640x jpg" -}}
<picture>
<source
type="image/webp"
data-srcset="
{{- with $respSizes -}}
{{- range $i, $e := . -}}
{{- if ge $src.Width . -}}
{{- if $i }}, {{ end -}}{{- ($src.Resize (printf "%sx%s" . " webp") ).RelPermalink }} {{ . }}w
{{- end -}}
{{- end -}}
{{- end -}}"
data-sizes="{{ $dataSzes }}"
/>
<source
type="image/jpeg"
data-srcset="
{{- with $respSizes -}}
{{- range $i, $e := . -}}
{{- if ge $src.Width . -}}
{{- if $i }}, {{ end -}}{{- ($src.Resize (printf "%sx%s" . " jpg") ).RelPermalink }} {{ . }}w
{{- end -}}
{{- end -}}
{{- end -}}"\
data-sizes="{{ $dataSzes }}"
/>
<img class="{{ $imgClass }}"
src="{{ $actualImg.RelPermalink }}"
width="{{ $src.Width }}"
height="{{ $src.Height }}"
alt="{{ $alt }}"
title="{{ .Title }}"
/>
</picture>
{{- else -}}
<img extra="MISSING" src="{{ .Destination | safeURL }}"
{{- with .Text }} alt="{{ . }}"{{ end -}}
{{- with .Title }} title="{{ . }}"{{ end -}}
>
{{- end -}}
That can be called from a template like this:
{{ partial "responsive-img.html" (dict
"img" .Site.Params.author_section.author_section_image
"alt" .Site.Params.author_section.author_section_image_alt
) }}
Then for shortcode usage with two simple parameters, that looks like:
/layouts/shortcodes/img.html
{{- $imgBase := "" -}}
{{- $src := resources.Get (printf "%s%s" $imgBase (.Get "src")) -}}
{{- $alt := .Get "alt" -}}
{{ partial "responsive-img.html" (dict
"img" $src
"alt" $alt
) }}
Which can be called from a template with arguments that looks like this:
{{< img src="/images/bobs-big-boy.jpg" alt="Bob's Big Boy" >}}
And finally, for overriding the default markdown, there’s the render-image.html override:
/layouts/_default/_markup/render-image.html
{{ partial "responsive-img.html" (dict
"img" .Destination
"alt" .Text
"caption" .Title
) }}
And the markdown code for that remains as documented:
![Bob's Big Boy](/images/bobs-big-boy.jpg "Bob's Big Boy")
I’m still hoping to get one of the expertly written responsive image modules working someday when I understand the errors a little better, but for now this looks like it’s generating mostly good code!
Thanks
Rob
P.S. Thank you to Brycewray.com for the post that got me started on this.