Created
August 14, 2018 11:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{- $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