Advertisement
Guest User

Untitled

a guest
Oct 9th, 2016
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. {{ $src := .Get "src" }}
  2. {{ $name := substr $src 0 -4 }}
  3. {{ $ext := substr $src (sub (len $src) 4) }}
  4. {{ $.Scratch.Set "maxsize" 0 }}
  5. {{ $.Scratch.Set "maxsrc" "" }}
  6.  
  7. {{ range .Page.Site.Data.images }}
  8. {{ if (isset . "width") and (isset . "height") }}
  9. {{ $.Scratch.Set "subname" (printf "%v-%vx%v" $name .width .height) }}
  10. {{ $.Scratch.Set "spec" (printf "%vw" .width) }}
  11. {{ else if isset . "width" }}
  12. {{ $.Scratch.Set "subname" (printf "%v-%v" $name .width) }}
  13. {{ $.Scratch.Set "spec" (printf "%vw" .width) }}
  14. {{ else if isset . "height" }}
  15. {{ $.Scratch.Set "subname" (printf "%v-%v" $name .height) }}
  16. {{ $.Scratch.Set "spec" (printf "%vh" .height) }}
  17. {{ end }}
  18.  
  19. {{ with .name }}
  20. {{ $.Scratch.Set "subname" (printf "%v-%v" $name .) }}
  21. {{ end }}
  22. {{ with .suffix }}
  23. {{ $.Scratch.Set "subname" (printf "%v_%v" ($.Scratch.Get "subname") .) }}
  24. {{ end }}
  25.  
  26. {{ $path := (printf "/img/responsive/%v%v" ($.Scratch.Get "subname") $ext)}}
  27. {{ $.Scratch.SetInMap "imgs" .name (printf "%v %v" $path ($.Scratch.Get "spec")) }}
  28.  
  29. {{ if not ($.Scratch.Get "maxsrc") }}
  30. {{ $.Scratch.Set "maxsrc" $path }}
  31. {{ else if and (isset . "height") (gt .height ($.Scratch.Get "maxsize")) }}
  32. {{ $.Scratch.Set "maxsrc" $path }}
  33. {{ $.Scratch.Set "maxsize" .height }}
  34. {{ else if and (isset . "width") (gt .width ($.Scratch.Get "maxsize")) }}
  35. {{ $.Scratch.Set "maxsrc" $path }}
  36. {{ $.Scratch.Set "maxsize" .width }}
  37. {{ end }}
  38. {{ end }}
  39. {{ $srcset := delimit ($.Scratch.GetSortedMapValues "imgs") ", " }}
  40. {{ 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