gabrielgio.me @ eaad0396c0d18ed2af7713aed1fd4a9065e9388b

 1{{/* 
 2    GetFeaturedImage
 3
 4    This partial gets the url for featured image for a given page. 
 5    
 6    If a featured_image was set in the page's front matter, then that will be used.
 7
 8    If not set, this will search page resources to find an image that contains the word
 9    "cover", and if found, returns the path to that resource.
10
11    If no featured_image was set, and there's no "cover" image in page resources, then
12    this partial returns an empty string (which evaluates to false).
13
14    @return Permalink to featured image, or an empty string if not found.
15
16*/}}
17
18{{/* Declare a new string variable, $linkToCover */}}
19{{ $linkToCover := "" }}
20
21{{/* Use the value from front matter if present */}}
22{{ if .Params.featured_image }}
23    {{ $linkToCover = .Params.featured_image }}
24
25{{/* Find the first image with 'cover' in the name in this page bundle. */}}
26{{ else }}
27    {{ $img := (.Resources.ByType "image").GetMatch "*cover*" }}
28    {{ with $img }}
29        {{ $linkToCover = .Permalink }}
30    {{ end }}
31{{ end }}
32
33{{/* return either a permalink, or an empty string. Note that partials can only have a single
34return statement, so this needs to be at the end of the partial (and not in the if block) */}}
35{{ return $linkToCover }}