Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {{ $src := .Get "src" }}
- {{ $name := substr $src 0 -4 }}
- {{ $ext := substr $src (sub (len $src) 4) }}
- {{ $.Scratch.Set "maxsize" 0 }}
- {{ $.Scratch.Set "maxsrc" "" }}
- {{ range .Page.Site.Data.images }}
- {{ if (isset . "width") and (isset . "height") }}
- {{ $.Scratch.Set "subname" (printf "%v-%vx%v" $name .width .height) }}
- {{ $.Scratch.Set "spec" (printf "%vw" .width) }}
- {{ else if isset . "width" }}
- {{ $.Scratch.Set "subname" (printf "%v-%v" $name .width) }}
- {{ $.Scratch.Set "spec" (printf "%vw" .width) }}
- {{ else if isset . "height" }}
- {{ $.Scratch.Set "subname" (printf "%v-%v" $name .height) }}
- {{ $.Scratch.Set "spec" (printf "%vh" .height) }}
- {{ end }}
- {{ with .name }}
- {{ $.Scratch.Set "subname" (printf "%v-%v" $name .) }}
- {{ end }}
- {{ with .suffix }}
- {{ $.Scratch.Set "subname" (printf "%v_%v" ($.Scratch.Get "subname") .) }}
- {{ end }}
- {{ $path := (printf "/img/responsive/%v%v" ($.Scratch.Get "subname") $ext)}}
- {{ $.Scratch.SetInMap "imgs" .name (printf "%v %v" $path ($.Scratch.Get "spec")) }}
- {{ if not ($.Scratch.Get "maxsrc") }}
- {{ $.Scratch.Set "maxsrc" $path }}
- {{ else if and (isset . "height") (gt .height ($.Scratch.Get "maxsize")) }}
- {{ $.Scratch.Set "maxsrc" $path }}
- {{ $.Scratch.Set "maxsize" .height }}
- {{ else if and (isset . "width") (gt .width ($.Scratch.Get "maxsize")) }}
- {{ $.Scratch.Set "maxsrc" $path }}
- {{ $.Scratch.Set "maxsize" .width }}
- {{ end }}
- {{ end }}
- {{ $srcset := delimit ($.Scratch.GetSortedMapValues "imgs") ", " }}
- {{ printf "<img src=\"%v\" srcset=\"%v\" sizes=\"%v\">" ($.Scratch.Get "maxsrc") $srcset (.Get "sizes") | safeHTML }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement