use get-image partial

This commit is contained in:
Ben Goldsworthy 2025-06-04 17:57:21 +02:00
parent ed9e769b2d
commit edba5d203e
No known key found for this signature in database
8 changed files with 94 additions and 47 deletions

View file

@ -23,11 +23,14 @@
{{ if .IsDescendant ( .GetPage "/cv/organisations" ) }} {{ if .IsDescendant ( .GetPage "/cv/organisations" ) }}
<header class="site-content__header"> <header class="site-content__header">
<div class="article-header__featured-image article-header__featured-image--fit" <div class="article-header__featured-image article-header__featured-image--fit"
{{ if .Params.featured_image }} {{- with .Params.featured_image -}}
{{ with .Resources.GetMatch .Params.featured_image }} {{- with ( $.Resources.GetMatch . ) -}}
style="background-image: url({{ .RelPermalink }}); {{ with .Params.bg }}background-color: {{ . }};{{ end }}" style="background-image: url({{- partialCached "images/get-image.html" ( dict "img" . ) .Title -}}){{ with .Params.bg -}}; background-color: {{ . -}};{{- end -}}"
{{ end }} {{- else -}}
{{ end }}> {{- errorf "No Page Resource found for src '%q' (%q).\n\n\tAvailable Page Resources are:\n\t\t%v" . $.pc.Page.File.Path $.pc.Page.Resources -}}
{{- end -}}
{{- end -}}
>
<div class="article-header__title-wrapper{{ if .Params.title_in_logo }} article-header__title-wrapper--no-title{{ end }}"> <div class="article-header__title-wrapper{{ if .Params.title_in_logo }} article-header__title-wrapper--no-title{{ end }}">
<h2 class="article-header__title">{{ default .Title .Params.markup_title | .Page.RenderString }}</h2> <h2 class="article-header__title">{{ default .Title .Params.markup_title | .Page.RenderString }}</h2>
</div> </div>

View file

@ -1,25 +1,62 @@
{{- /* <!--
Renders an image, resizing it if valid. Renders an image, resizing it if valid.
@params img The image. @params img The image.
*/ @params size The resize target. Optional.
-}} @params format Image conversation format. Optional.
-->
<!-- Variable assignment -->
{{- $img := .img -}}
{{- $hint := default ( index $img.Params "hint" ) "" }}
{{- $size := cond ( isset . "size" ) .size "" }}
{{- $isResizable := not ( in ( slice "avif" "svg" "gif" ) $img.MediaType.SubType ) -}}
{{- $format := cond ( isset . "format" ) .format "webp" }}
{{- $permalink := "" -}}
<!-- Validation --> <!-- Validation -->
{{- if ( not ( isset . "img" ) ) -}}
{{- errorf "No img set" -}}
{{- end -}}
{{- if ne .img.MediaType.MainType "image" -}} {{- if ne .img.MediaType.MainType "image" -}}
{{- errorf "Image '%q' is not an image" .img }} {{- errorf "Image '%q' is not an image" .img }}
{{- end -}} {{- end -}}
{{- if ( isset . "size" ) -}}
{{- if $isResizable -}}
{{- $width := ( first 1 ( split .size "x" ) ) -}}
{{- if gt $width $img.Width -}}
{{- warnf "Requested size %s is more than image width %d (%q)" $width $img.Width $img.File.Path -}}
{{- end -}}
{{- else -}}
{{- warnf "Size set on non-resizable image of type %s; ignoring" $img.MediaType.SubType -}}
{{- end -}}
{{- end -}}
<!-- Rendering --> <!-- Rendering -->
{{- $isResizable := not ( in ( slice "avif" "svg" "gif" ) .img.MediaType.SubType ) -}}
{{- if $isResizable -}} {{- if $isResizable -}}
{{- with .img.Resize "1200x webp" -}} {{- if $size -}}
{{- .RelPermalink -}} {{- if eq $size "same" -}}
{{- $permalink = ( $img.Process ( printf "%s %s" $format $hint ) ).RelPermalink -}}
{{- else -}}
{{- $permalink = ( $img.Resize ( printf "%s %s %s" $size $format $hint ) ).RelPermalink -}}
{{- end -}}
{{- else -}}
{{- if gt $img.Width 1200 -}}
{{- $permalink = ( $img.Resize ( printf "1200x %s %s" $format $hint ) ).RelPermalink -}}
{{- else if gt $img.Width 800 -}}
{{- $permalink = ( $img.Resize ( printf "800x %s %s" $format $hint ) ).RelPermalink -}}
{{- else -}}
{{- $permalink = ( $img.Process ( printf "%s %s" $format $hint ) ).RelPermalink -}}
{{- end -}}
{{- end -}} {{- end -}}
{{- else }} {{- else }}
{{- .img.RelPermalink -}} {{- $permalink = $img.RelPermalink -}}
{{- end -}} {{- end -}}
{{- return $permalink -}}

View file

@ -32,17 +32,14 @@
<article <article
class="item-tile lazy {{- with .Params.site }}item-tile--{{ . }}{{ end -}}" class="item-tile lazy {{- with .Params.site }}item-tile--{{ . }}{{ end -}}"
{{ if .Params.featured_image }} {{ if .Params.featured_image }}
style="background-image: url(
{{ with .Resources.GetMatch .Params.featured_image }} {{ with .Resources.GetMatch .Params.featured_image }}
{{ if ( in ( slice "avif" "svg" "gif" ) .MediaType.SubType ) }} {{- partialCached "images/get-image.html" ( dict "img" . "size" "500x" ) ( printf "%s-500x" .Title ) -}}
style="background-image: url({{ .RelPermalink }})" {{- else -}}
{{ else }} {{- warnf "Featured image %s is not a page resource" .Params.featured_image -}}
{{ with .Resize "1000x webp" }} {{- .Params.featured_image -}}
style="background-image: url({{ .RelPermalink }})" {{- end -}}
{{ end }} )"
{{ end }}
{{ else }}
style="background-image: url({{ .Params.featured_image }})"
{{ end }}
{{ end }} {{ end }}
> >
{{ if .Params.featured_image -}} {{ if .Params.featured_image -}}
@ -51,15 +48,10 @@
style="display: none;" style="display: none;"
src=" src="
{{- with .Resources.GetMatch .Params.featured_image -}} {{- with .Resources.GetMatch .Params.featured_image -}}
{{- if ( in ( slice "avif" "svg" "gif" ) .MediaType.SubType ) -}} {{- partialCached "images/get-image.html" ( dict "img" . "size" "500x" ) ( printf "%s-500x" .Title ) -}}
{{ .RelPermalink }}
{{- else -}} {{- else -}}
{{- with .Resize "1000x webp" -}} {{- warnf "Featured image %s is not a page resource" .Params.featured_image -}}
{{- .RelPermalink -}} {{- .Params.featured_image -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{ .Params.featured_image }}
{{ end }} {{ end }}
" "
alt=" alt="

View file

@ -1,12 +1,15 @@
<!-- <!--
Render an image in a context-appropriate format and size. Render an image in a context-appropriate format and size.
@params context Image media file. @params src Image media file.
@params raw Force raw image only. Optional.
--> -->
<!-- Variable assignment --> <!-- Variable assignment -->
{{- $src := . -}} {{- $src := .src -}}
{{- $hint := default ( index $src.Params "hint" ) "" }}
{{- $format := cond ( and ( isset . "raw" ) .raw ) "" "webp" -}}
{{- $isResizable := not ( in ( slice "svg" "gif" ) $src.MediaType.SubType ) -}} {{- $isResizable := not ( in ( slice "svg" "gif" ) $src.MediaType.SubType ) -}}
@ -17,18 +20,30 @@
itemprop="image" itemprop="image"
> >
{{ if $isResizable -}} {{ if $isResizable -}}
<source srcset="{{- ($src.Resize "1200x webp").RelPermalink -}}" />
<source <source
srcset="{{- ($src.Resize "800x png").RelPermalink -}}" srcset="{{- partialCached "images/get-image.html" ( dict "img" $src "size" "same" "format" $format ) $src.Title -}}"
/>
{{- if gt $src.Width 1200 -}}
<source
srcset="{{- partialCached "images/get-image.html" ( dict "img" $src "size" "1200x" "format" $format ) ( printf "%s-1200x" $src.Title ) -}}"
media="(max-width: 1200px)"
/>
{{- end -}}
{{- if gt $src.Width 800 -}}
<source
srcset="{{- partialCached "images/get-image.html" ( dict "img" $src "size" "800x" "format" $format ) ( printf "%s-800x" $src.Title ) -}}"
media="(max-width: 800px)" media="(max-width: 800px)"
/> />
{{- end -}}
{{- end }} {{- end }}
<img <img
class="u-photo picture__image" class="u-photo picture__image"
{{ if not $isResizable -}} {{ if not $isResizable -}}
src="{{- $src.RelPermalink -}}" src="{{- $src.RelPermalink -}}"
{{- else -}} {{- else -}}
src="{{- ($src.Resize "1200x webp").RelPermalink -}}" width="{{- $src.Width -}}" height="{{- $src.Height -}}" src="{{- partialCached "images/get-image.html" ( dict "img" $src "format" $format ) $src.Title -}}"
width="{{- $src.Width -}}"
height="{{- $src.Height -}}"
{{- end }} {{- end }}
{{ with $src.Params.alt -}}alt="{{- . -}}"{{- end }} {{ with $src.Params.alt -}}alt="{{- . -}}"{{- end }}
{{ with $src.Params.title -}}title="{{- . -}}"{{- end }} {{ with $src.Params.title -}}title="{{- . -}}"{{- end }}

View file

@ -16,9 +16,7 @@
poster=" poster="
{{- if $src.Params.poster -}} {{- if $src.Params.poster -}}
{{- $posterSrc := $.Page.Resources.GetMatch ( .Get $src.Params.poster ) -}} {{- $posterSrc := $.Page.Resources.GetMatch ( .Get $src.Params.poster ) -}}
{{- with $posterSrc.Resize "1200x webp" -}} {{- partialCached "images/get-image.html" ( dict "img" $posterSrc ) $posterSrc.Title -}}
{{- .RelPermalink -}}
{{- end -}}
{{- else -}} {{- else -}}
{{- erroridf "a11y-video-poster" "No poster defined for resource %q (%q)" $src.RelPermalink .page_file_path -}} {{- erroridf "a11y-video-poster" "No poster defined for resource %q (%q)" $src.RelPermalink .page_file_path -}}
{{- end -}} {{- end -}}

View file

@ -20,7 +20,7 @@
style="display: none;" style="display: none;"
src=" src="
{{- with ( $.pc.Resources.GetMatch . ) -}} {{- with ( $.pc.Resources.GetMatch . ) -}}
{{ partialCached "images/get-image.html" ( dict "img" . ) . }} {{- partialCached "images/get-image.html" ( dict "img" . ) .Title -}}
{{- else -}} {{- else -}}
{{- . -}} {{- . -}}
{{- end -}} {{- end -}}
@ -37,7 +37,7 @@
class="article-header__featured-image" class="article-header__featured-image"
{{ with .pc.Params.featured_image }} {{ with .pc.Params.featured_image }}
{{ with ( $.pc.Resources.GetMatch . ) }} {{ with ( $.pc.Resources.GetMatch . ) }}
style="background-image: url({{ partial "images/get-image.html" ( dict "img" . ) }})" style="background-image: url({{- partialCached "images/get-image.html" ( dict "img" . ) .Title -}})"
{{ else }} {{ else }}
{{- if $.pc.Page.Resources -}} {{- if $.pc.Page.Resources -}}
{{- errorf "No Page Resource found for src '%q' (%q).\n\n\tAvailable Page Resources are:\n\t\t%v" . $.pc.Page.File.Path $.pc.Page.Resources -}} {{- errorf "No Page Resource found for src '%q' (%q).\n\n\tAvailable Page Resources are:\n\t\t%v" . $.pc.Page.File.Path $.pc.Page.Resources -}}

View file

@ -40,7 +40,7 @@
{{- end -}} {{- end -}}
{{- if eq $src.ResourceType "image" -}} {{- if eq $src.ResourceType "image" -}}
{{- partial "media/picture.html" $src -}} {{- partial "media/picture.html" ( dict "src" $src ) -}}
{{- else if eq $src.ResourceType "audio" -}} {{- else if eq $src.ResourceType "audio" -}}
{{- partial "media/audio.html" $src -}} {{- partial "media/audio.html" $src -}}

View file

@ -4,11 +4,13 @@
@params src Image `src` path. @params src Image `src` path.
@params link URI or Page Resource to wrap image in. Optional. @params link URI or Page Resource to wrap image in. Optional.
@params rel Force relationship value. Optional. @params rel Force relationship value. Optional.
@params raw Force raw image only. Optional.
--> -->
<!-- Variable assignment --> <!-- Variable assignment -->
{{- $src := $.Page.Resources.GetMatch ( .Get "src" ) -}} {{- $src := $.Page.Resources.GetMatch ( .Get "src" ) -}}
{{- $raw := default false ( .Get "raw" ) -}}
{{- $linkedResource := $.Page.Resources.GetMatch ( .Get "link" ) -}} {{- $linkedResource := $.Page.Resources.GetMatch ( .Get "link" ) -}}
{{- $valid_rel_values := partialCached "util/get_valid_rel_values.html" . -}} {{- $valid_rel_values := partialCached "util/get_valid_rel_values.html" . -}}
@ -37,7 +39,7 @@
<a href="{{- .Get "link" -}}"> <a href="{{- .Get "link" -}}">
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- partial "media/picture.html" $src -}} {{- partial "media/picture.html" ( dict "src" $src "raw" $raw ) -}}
{{- if .Get "link" -}} {{- if .Get "link" -}}
</a> </a>
{{- end -}} {{- end -}}