kobel/layouts/partials/components/image.html

41 lines
919 B
HTML
Raw Normal View History

2023-12-23 23:01:53 +00:00
<!--
type: string - default
url: string
alt: string
-->
{{ $avatar := "avatar"}}
{{ $sizes := slice "320" "640" "960" "1280" }}
{{ $ext := path.Ext .url }}
{{ $name := path.Base (replace .url $ext "") }}
{{ $dir := path.Dir .url }}
<picture>
<source
type="image/avif"
srcset="
{{ range $i, $size := $sizes }}
{{ $dir }}/{{ $name }}-{{ $size }}{{ $ext }}.avif {{ $size }}w,
{{ end }}
{{ .url }}.avif
"
/>
<source
type="image/webp"
srcset="
{{ range $i, $size := $sizes }}
{{ $dir }}/{{ $name }}-{{ $size }}{{ $ext }}.webp {{ $size }}w,
{{ end }}
{{ .url }}.webp
"
/>
<img
class="{{if eq .type $avatar}}avatar{{end}}"
alt="{{ .alt }}"
src="{{ .url }}"
loading="lazy"
decoding="async"
>
</picture>
2024-01-06 19:44:35 +00:00
{{/* https://www.brycewray.com/posts/2023/05/better-code-image-processing-hugo-render-hook-edition/ */}}