In a theme using a custom image render hook, a srcset
attribute of several resized images is used, along with its sizes
attribute. It is quite handy to optimize resources and page loading.
Say some pages have content with full-width images, while some others have “blocks” with half-width images. Some logic to change the srcset
and sizes
attributes in the image render hook depending on the case (“block with half-width images” or no “block”) would be convenient, so images can be appropriately resized.
The theme has a custom content.html
partial that sets some HTML semantics (like <section>
, <article>
) depending on the Markdown content, useful to style “blocks” among others. I’d like to reuse this separation code to pass a variable to the render hook, so srcset
and sizes
can be set appropriately.
I’ve tried to use the Store method (or Scratch) without success:
in partials/content.html
:
{{/* If blocks */}} {{ .Store.Set "blocks" true }}
and then in _default/_markup/render-image.html
:
{{- if .Store.Get "blocks" -}} {{/* Set srcset and sizes */}}
I would like to not use shortcodes. How would you approach this instead ? Thank you !
Local env
hugo v0.129.0-e85be29867d71e09ce48d293ad9d1f715bc09bb9 linux/amd64 BuildDate=2024-07-17T13:29:16Z VendorInfo=gohugoio
GOOS="linux"
GOARCH="amd64"
GOVERSION="go1.22.2"