diff --git a/modules/blox/layouts/_partials/functions/get_featured_image.html b/modules/blox/layouts/_partials/functions/get_featured_image.html index 11d71df9..a48d832f 100644 --- a/modules/blox/layouts/_partials/functions/get_featured_image.html +++ b/modules/blox/layouts/_partials/functions/get_featured_image.html @@ -13,10 +13,8 @@ {{ $resource := (.Resources.ByType "image").GetMatch "*featured*" }} {{ if not (reflect.IsResource $resource) }} {{/* Otherwise fall back the image file specified in front matter */}} - {{ $filename := "" }} - {{ if and .Params.image (reflect.IsMap .Params.image) }} - {{ $filename = .Params.image.filename }} - {{ end }} + {{ $image_params := partial "functions/get_page_image_params.html" . }} + {{ $filename := $image_params.filename | default "" }} {{/* Search in page folder */}} {{ $resource = (.Resources.ByType "image").GetMatch $filename }} {{/* Otherwise in `assets/media/` folder */}} diff --git a/modules/blox/layouts/_partials/functions/get_page_image_params.html b/modules/blox/layouts/_partials/functions/get_page_image_params.html new file mode 100644 index 00000000..a845da40 --- /dev/null +++ b/modules/blox/layouts/_partials/functions/get_page_image_params.html @@ -0,0 +1,12 @@ +{{/* Return the page's `image` frontmatter as a map, or an empty map if unset. + Warns if `image` is set but not an object. */}} +{{ $img := .Params.image }} +{{ $result := dict }} +{{ if $img }} + {{ if reflect.IsMap $img }} + {{ $result = $img }} + {{ else }} + {{ warnf "page %q: `image` frontmatter must be an object, e.g.\n image:\n filename: cover.jpg\nGot: %v" .Path $img }} + {{ end }} +{{ end }} +{{ return $result }} diff --git a/modules/blox/layouts/_partials/views/card.html b/modules/blox/layouts/_partials/views/card.html index 46e8ac12..1e2c7871 100644 --- a/modules/blox/layouts/_partials/views/card.html +++ b/modules/blox/layouts/_partials/views/card.html @@ -8,10 +8,8 @@ {{ end }} {{ $resource := partial "functions/get_featured_image.html" $item }} -{{ $anchor := "Center" }} -{{ if and $item.Params.image (reflect.IsMap $item.Params.image) }} - {{ $anchor = $item.Params.image.focal_point | default "Center" }} -{{ end }} +{{ $image_params := partial "functions/get_page_image_params.html" $item }} +{{ $anchor := $image_params.focal_point | default "Center" }} {{ $fill_image := .config.fill_image | default true }} {{ $showDate := .config.show_date | default true }} {{ $showReadTime := .config.show_read_time | default true }} diff --git a/modules/blox/layouts/events/page.html b/modules/blox/layouts/events/page.html index 3c4fee7f..a57193ee 100644 --- a/modules/blox/layouts/events/page.html +++ b/modules/blox/layouts/events/page.html @@ -141,11 +141,7 @@ {{ else }} {{/* Fallback to featured image if no slides */}} {{ $featured := partial "functions/get_featured_image.html" . }} - {{/* Safely extract image parameters - handle case where image is not a map */}} - {{ $image_params := dict }} - {{ if and .Params.image (reflect.IsMap .Params.image) }} - {{ $image_params = .Params.image }} - {{ end }} + {{ $image_params := partial "functions/get_page_image_params.html" . }} {{ if and $featured (not $image_params.preview_only) }} {{/* Fit image within max size. */}} {{ $image := $featured }} diff --git a/modules/blox/layouts/single.html b/modules/blox/layouts/single.html index e5410d84..c7d9b787 100644 --- a/modules/blox/layouts/single.html +++ b/modules/blox/layouts/single.html @@ -147,11 +147,7 @@ {{ $featured := partial "functions/get_featured_image.html" . }} - {{/* Safely extract image parameters - handle case where image is not a map */}} - {{ $image_params := dict }} - {{ if and .Params.image (reflect.IsMap .Params.image) }} - {{ $image_params = .Params.image }} - {{ end }} + {{ $image_params := partial "functions/get_page_image_params.html" . }} {{/* Featured image layout */}} {{ if and $featured (not $image_params.preview_only) }} {{/* Fit image within max size. */}}