Files
hugo-theme-stack/layouts/_partials/helper/image.html
T

54 lines
1.5 KiB
HTML
Raw Normal View History

/// Params:
/// Resources: Where to search for images
/// Image: Image URL
/// Returns:
/// Resource: Hugo image resource object
/// Permalink: Image URL
/// Height: Image height
/// Width: Image width
2020-09-11 18:13:39 +02:00
2026-01-30 15:07:14 +01:00
{{ $result := "" }}
2026-01-30 15:07:14 +01:00
{{ if .Image }}
{{ $url := urls.Parse .Image }}
{{ $permalink := .Image }}
2026-01-30 15:07:14 +01:00
{{ $resource := "" }}
{{ if not (in (slice "https" "http") $url.Scheme) }}
{{/* Local image */}}
2026-01-30 15:07:14 +01:00
{{ $resource = .Resources.Get (printf "%s" (.Image | safeURL)) }}
{{ $permalink = $resource.RelPermalink }}
2026-01-30 15:07:14 +01:00
{{ else }}
{{/* Remote image */}}
2026-01-30 15:07:14 +01:00
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
{{ $resource = . }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
{{ end }}
{{ with $resource }}
{{ if reflect.IsImageResourceProcessable . }}
{{ $result = dict
2026-01-30 15:07:14 +01:00
"Resource" $resource
"Permalink" $permalink
"Height" $resource.Height
"Width" $resource.Width
}}
{{ else }}
{{ $result = dict
"Resource" $resource
"Permalink" $permalink
"Height" nil
"Width" nil
}}
{{ end }}
{{ end }}
2020-09-10 18:53:36 +02:00
{{ end }}
{{ return $result }}