Browse Source

better whitespace in image.html

Alexander Avery 3 weeks ago
parent
commit
7ebf7c33af
  1. 58
      layouts/partials/image.html

58
layouts/partials/image.html

@ -6,40 +6,36 @@ Renders an image with the given class, alt, and sizes.
@context {classes} classes for the image. @context {classes} classes for the image.
*/}} */}}
{{- $alt := .alt }}
{{- $image := .image }}
{{- $sizes := .sizes }}
{{- $classes := .classes }}
{{ $alt := .alt }} {{- $srcsetParts := slice }}
{{ $image := .image }} {{- $sizesParts := slice }}
{{ $sizes := .sizes }} {{- $len := len $sizes }}
{{ $classes := .classes }}
<img class="{{ delimit $classes " " }}"
{{ $srcsetParts := slice }}
{{ $sizesParts := slice }}
{{ $len := len $sizes }}
{{/* Loop through widths to resize images and build attributes */}} {{/* Loop through widths to resize images and build attributes */}}
{{ range $index, $width := $sizes }} {{- range $index, $width := $sizes }}
{{ $resizeSpec := printf "%dx webp q90" $width }} {{- $resizeSpec := printf "%dx webp q90" $width }}
{{ $resized := ($image.Resize $resizeSpec) }} {{- $resized := ($image.Resize $resizeSpec) }}
{{ $srcsetParts = $srcsetParts | append (printf "%s %dw" $resized.RelPermalink $width) }} {{- $srcsetParts = $srcsetParts | append (printf "%s %dw" $resized.RelPermalink $width) }}
{{/* Build sizes: media query for all but the last width */}} {{- /* Build sizes: media query for all but the last width */}}
{{ if lt $index (sub $len 1) }} {{- if lt $index (sub $len 1) }}
{{ $nextWidth := index $sizes (add $index 1) }} {{- $nextWidth := index $sizes (add $index 1) }}
{{ $sizesParts = $sizesParts | append (printf "(max-width: %dpx) %dpx" $nextWidth $width) }} {{- $sizesParts = $sizesParts | append (printf "(max-width: %dpx) %dpx" $nextWidth $width) }}
{{ else }} {{- else }}
{{ $sizesParts = $sizesParts | append (printf "%dpx" $width) }} {{- $sizesParts = $sizesParts | append (printf "%dpx" $width) }}
{{ end }} {{- end }}
{{ end }} {{- end }}
{{/* Set src to the smallest image */}} {{- $smallestWidth := index $sizes 0 }}
{{ $smallestWidth := index $sizes 0 }} {{- $smallestResizeSpec := printf "%dx webp q90" $smallestWidth }}
{{ $smallestResizeSpec := printf "%dx webp q90" $smallestWidth }} {{- $smallest := ($image.Resize $smallestResizeSpec) }}
{{ $smallest := ($image.Resize $smallestResizeSpec) }} {{- $src := $smallest.RelPermalink }}
{{ $src := $smallest.RelPermalink }} <img class="{{ delimit $classes " " }}"
{{/* Output attributes */}}
src="{{ $src }}" src="{{ $src }}"
srcset="{{ delimit $srcsetParts ", " }}" srcset="{{ delimit $srcsetParts ", " }}"
sizes="{{ delimit $sizesParts ", " }}" sizes="{{ delimit $sizesParts ", " }}"

Loading…
Cancel
Save