gohugo-theme-ananke/layouts/partials/func/GetFeaturedImage.html
2022-04-21 19:38:21 -07:00

49 lines
1.7 KiB
HTML

{{/*
GetFeaturedImage
This partial gets the 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 false.
@return RelPermalink to featured image, or an empty string if not found.
*/}}
{{/* Declare a new variable, $coverImage */}}
{{ $coverImage := false }}
{{ $matches := "feature,cover" }}
{{/* Use the value from front matter if present */}}
{{ with .Params.featured_image }}
{{/* If we find a remote global Resource matching the exact value, we use it. */}}
{{ if (hasPrefix . "http") }}
{{ with resources.GetRemote . }}
{{ $coverImage = . }}
{{ end }}
{{ else }}
{{/* If we find a local global Resource matching the exact value, we use it. */}}
{{ with resources.GetMatch . }}
{{ $coverImage = . }}
{{ end }}
{{ end }}
{{/* If we find a Page Resource matching the exact value, we use it instead. */}}
{{ with $.Resources.GetMatch . }}
{{ $coverImage = . }}
{{ end }}
{{/* Find the first image with 'cover' in the name in this page bundle. */}}
{{ else }}
{{ with .Resources.ByType "image" }}
{{ with .GetMatch (printf "**{%s}*" $matches) }}
{{ $coverImage = . }}
{{ end }}
{{ end }}
{{ end }}
{{/* Return either an image or false. 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 $coverImage }}