Skip to content

Commit

Permalink
Add responsive image rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
chringel21 committed Aug 25, 2022
1 parent 34cfe9e commit d8150fd
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions layouts/_default/_markup/render-image.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
{{ if .Title }}
{{ $image := .Page.Resources.GetMatch .Destination }}
{{ $alt := .Title | default .Text }}

{{ $tinyw := default "500x webp" }}
{{ $smallw := default "800x webp" }}
{{ $mediumw := default "1200x webp" }}
{{ $largew := default "1500x webp" }}

{{ $data := newScratch }}
{{ $data.Set "tiny" ($image.Resize $tinyw) }}
{{ $data.Set "small" ($image.Resize $smallw) }}
{{ $data.Set "medium" ($image.Resize $mediumw) }}
{{ $data.Set "large" ($image.Resize $largew) }}

{{ $tiny := $data.Get "tiny" }}
{{ $small := $data.Get "small" }}
{{ $medium := $data.Get "medium" }}
{{ $large := $data.Get "large" }}


<a href="{{ $image.RelPermalink }}">
<figure>
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" loading="lazy" />
<picture>
<source
media="(max-width: 376px)"
srcset="{{ with $tiny.RelPermalink }}{{ . }}{{ end }}"
/>

<source
media="(max-width: 992px)"
srcset="{{ with $small.RelPermalink }}{{ . }}{{ end }}"
/>

<source
media="(max-width: 1400px)"
srcset="{{ with $medium.RelPermalink }}{{ . }}{{ end }}"
/>

<source
media="(min-width: 1600px)"
srcset="{{ with $large.RelPermalink }}{{ . }}{{ end }}"
/>

<img
alt="{{ $alt }}"
title="{{ $alt }}"
src="{{ $image }}"
height="{{ $image.Height }}"
width="{{ $image.Width }}"
loading="lazy"
/>
</picture>
<figcaption class="text-sm italic">
{{ if .Title }}
{{ .Title | $.Page.RenderString }}
{{ else }}
{{ .Text | $.Page.RenderString }}
{{ end }}
{{ $alt | $.Page.RenderString }}
</figcaption>
</figure>
{{ else }}
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" loading="lazy" />
{{ end }}
</a>

0 comments on commit d8150fd

Please sign in to comment.