erynwells.me/layouts/shortcodes/figures/image.html

51 lines
1.8 KiB
HTML

{{- $imgs := slice -}}
{{- $class := .Get "class" -}}
{{- $shouldResize := .Get "shouldResize" | default true -}}
{{- $shouldShowTitle := (.Get "shouldShowTitle") | default true -}}
{{- if .Get "name" -}}
{{- $imgs = $imgs | append (.Page.Resources.GetMatch (.Get "name")) -}}
{{- else if .Get "names" -}}
{{- range (split (.Get "names") ",") -}}
{{- $imgs = $imgs | append ($.Page.Resources.GetMatch .) -}}
{{- end -}}
{{- else -}}
{{ errorf "Missing name or names parameter to figures/image" }}
{{- end -}}
{{- if lt (len $imgs) 0 -}}
{{ errorf "No images found for name(s) parameter" }}
{{- end -}}
{{ with $item := index $imgs 0 }}
<figure class="figure figure--{{ $item.ResourceType }}{{ with $class }} {{ . }}{{ end }}">
<div class="figure__container">
{{ if eq $item.ResourceType "image" }}
{{- $processedImage := $item -}}
{{- if $shouldResize -}}
{{ $processedImage = $item.Fit "1280x1280" }}
{{- end -}}
{{- $altText := $item.Params.alt | default $item.Title -}}
<a href="{{ $item.RelPermalink }}">
<img src="{{ $processedImage.RelPermalink }}"
{{- with $altText }} alt="{{ . }}"{{- end }}
{{- with $item.Title }} title="{{ . }}"{{- end }}>
</a>
{{ else if eq $item.ResourceType "video" }}
<video controls width="100%">
<source src="{{ $item.RelPermalink }}" type="{{ $item.MediaType.Type }}">
</video>
{{- end }}
</div>
{{ if $shouldShowTitle -}}
{{- with $item -}}
{{- if gt (len .Title) 0 -}}
<figcaption>{{ .Title | markdownify }}</figcaption>
{{- end -}}
{{- end -}}
{{- end }}
</figure>
{{ else }}
{{ errorf "Found nil in items list for %s" $.Page.Permalink }}
{{ end }}