gohugo-theme-ananke/layouts/partials/func/GetFeaturedImage.html
Steven G. Harms 0f40733d46 Changes .GetMatch Pattern to find bundled assets
When I attempted to use `func/GetFeaturedImage.html`, it did not find my resource, `images/foo-cover.webp`. Updating the match syntax to the pattern expressed here, provided expected behavior:

Per:

https://gohugo.io/content-management/page-resources/#pattern-matching

> .Resources.Match "*sunset.jpg" 🚫

and...

> .Resources.Match "*" 🚫

Are not expected to work

> .Resources.Match "**/sunset.jpg" 

which follows this change, does.
2021-03-29 07:11:56 -04:00

35 lines
1.2 KiB
HTML

{{/*
GetFeaturedImage
This partial gets the url for featured image for a given page.
If a featured_image was set in the page's front matter, then that will be used.
If not set, this will search page resources to find an image that contains the word
"cover", and if found, returns the path to that resource.
If no featured_image was set, and there's no "cover" image in page resources, then
this partial returns an empty string (which evaluates to false).
@return Permalink to featured image, or an empty string if not found.
*/}}
{{/* Declare a new string variable, $linkToCover */}}
{{ $linkToCover := "" }}
{{/* Use the value from front matter if present */}}
{{ if .Params.featured_image }}
{{ $linkToCover = .Params.featured_image }}
{{/* Find the first image with 'cover' in the name in this page bundle. */}}
{{ else }}
{{ $img := (.Resources.ByType "image").GetMatch "**/*cover*" }}
{{ with $img }}
{{ $linkToCover = .Permalink }}
{{ end }}
{{ end }}
{{/* return either a permalink, or an empty string. Note that partials can only have a single
return statement, so this needs to be at the end of the partial (and not in the if block) */}}
{{ return $linkToCover }}