Hi! Very simply, all I want to do is build a shortcode I can insert in blogposts which uses the srcset and Hugo’s image resizing function to generate responsive images. I’m aware this has been discussed in other places, but I’m encountering a problem I can’t find solved anywhere else.
My shortcode is:
{{< image src="Bananas2.jpg" alt="alt goes here" caption="here's a caption" >}}
And inside my image.html I have the same code that is recommended on lots of articles and forums:
{{ $src := .Page.Resources.GetMatch (printf "*%s*" (.Get "src")) }}
{{ $tinyw := default "500x" }}
{{ $smallw := default "800x" }}
{{ $mediumw := default "1200x" }}
{{ $largew := default "1500x" }}
{{ .Scratch.Set "tiny" ($src.Resize $tinyw) }}
{{ .Scratch.Set "small" ($src.Resize $smallw) }}
{{ .Scratch.Set "medium" ($src.Resize $mediumw) }}
{{ .Scratch.Set "large" ($src.Resize $largew) }}
{{ $tiny := .Scratch.Get "tiny" }}
{{ $small := .Scratch.Get "small" }}
{{ $medium := .Scratch.Get "medium" }}
{{ $large := .Scratch.Get "large" }}
<img
{{ with .Get "sizes" }}sizes='{{.}}'{{ else }}sizes="(min-width: 35em) 1200px, 100vw"{{ end }}
srcset="
{{ if ge $src.Width "500" }}
{{ with $tiny.RelPermalink }}{{.}} 500w{{ end }}
{{ end }}
{{ if ge $src.Width "800" }}
{{ with $small.RelPermalink }}, {{.}} 800w{{ end }}
{{ end }}
{{ if ge $src.Width "1200" }}
{{ with $medium.RelPermalink }}, {{.}} 1200w{{ end }}
{{ end }}
{{ if ge $src.Width "1500" }}
{{ with $large.RelPermalink }}, {{.}} 1500w {{ end }}
{{ end }}"
{{ if .Get (print $medium) }}
src="{{ $medium.RelPermalink }}"
{{ else }}
src="{{ $src.RelPermalink }}"
{{ end }}/>
Except when I run hugo serve I get a text print out of the different images, not the images themselves. It looks like this:
It seems like Hugo is generating the different sized images, but doesn’t render them through the img tag?