Shortcode to generate images with srcset

Ok, I got something to work with this:

{{ $input := index .Params 0 }}{{ $img_folder := (print $.Page.URL "images/")}}{{ $img := (print $.Page.URL "images/"   $input)}}{{ $image_path := (print "content" $img)}}{{ with (imageConfig $image_path) }}{{ $width := .Width }}
{{ if ge $width $.Site.Params.maxWidth }}
 	<a href={{$img}}><img src={{$img}} srcset="{{$img_folder}}{{$.Site.Params.maxWidth}}/{{$input}} {{(printf "%dw"            $.Site.Params.maxWidth)}}
        	{{ range $index, $elem := $.Site.Params.imageWidths }},{{$img_folder}}{{$elem}}/{{$input}} {{(printf "%dw" $elem)}}
	        {{end}}" alt=""/></a>
{{ else }}<img src={{$img}} alt=""/>{{ end }}{{ end }}

Sorry for the weired formatting, but this way it takes out a bunch of whitespace from the generated html.

4 Likes