Skip to content

Instantly share code, notes, and snippets.

@myobie
Created August 14, 2018 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/8c7d441b3a7983aefba8551d043c7fc1 to your computer and use it in GitHub Desktop.
Save myobie/8c7d441b3a7983aefba8551d043c7fc1 to your computer and use it in GitHub Desktop.
Image short tag that supports retina images, page resources, and falls back to plain urls for Hugo websites
{{- $src := .Get "src" }}
{{- $s := newScratch }}
{{- if eq (len (findRE "^/" $src)) 0 }}
{{- $s.Set "res" ($.Page.Resources.GetMatch $src) }}
{{- with ($s.Get "res") }}
{{- $s.Set "src" .Permalink }}
{{- if ne (len (findRE "@2x" .Permalink)) 0 }}
{{- $s.Set "width" (div .Width 2) }}
{{- $s.Set "height" (div .Height 2) }}
{{- else }}
{{- $s.Set "width" .Width }}
{{- $s.Set "height" .Height }}
{{- end }}
{{- else }}
{{- $s.Set "src" ($src | absURL) }}
{{- end }}
{{- else }}
{{- $s.Set "src" ($src | absURL) }}
{{- end -}}
<img src="{{ $s.Get "src" }}"
{{- with .Get "class" }} class="{{.}}"{{ end }}
{{- with .Get "alt" }} alt="{{.}}"{{ end }}
{{- with $s.Get "width" }} width="{{.}}"{{ end }}
{{- with $s.Get "height" }} height="{{.}}"{{ end }}>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment