Skip to content

Instantly share code, notes, and snippets.

@robshep
Created April 14, 2019 11:22
Show Gist options
  • Save robshep/feb28db3f9af67ca55b9cb153f2ca6e3 to your computer and use it in GitHub Desktop.
Save robshep/feb28db3f9af67ca55b9cb153f2ca6e3 to your computer and use it in GitHub Desktop.
Testing out Hugo's image finding routine in the twitter_card shortcode
<!-- save in /layouts/shortcodes/tct.html -->
<!-- usage: {{< tct >}} -->
<div>
{{- with $.Page.Params.images -}}
<span>Image: page.params.images[0]: <code>{{ index . 0 | absURL }}</code> </span>
{{ else -}}
{{- $images := $.Page.Resources.ByType "image" -}}
{{- $featured := $images.GetMatch "*feature*" -}}
{{- $featured := cond (ne $featured nil) $featured ($images.GetMatch "{*cover*,*thumbnail*}") -}}
{{- with $featured -}}
<span>Image: page.resources./.*featured.*/: <code>{{ $featured.Permalink }}</code></span>
{{- else -}}
{{- with $.Site.Params.images -}}
<span>Image: site.params.images[0]: <code>{{ index . 0 | absURL }}</code></span>
{{ else -}}
{{- end -}}
{{- end -}}
{{- end }}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment