render-image.html 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. {{- /* Determine content path for error messages. */}}
  2. {{- $path := "" }}
  3. {{- with .Page.File }}
  4. {{- $path = .Path }}
  5. {{- else }}
  6. {{- $path = .Path }}
  7. {{- end }}
  8. {{- /* Get image resource. */}}
  9. {{- $u := urls.Parse .Destination }}
  10. {{- $r := "" }}
  11. {{- if $u.IsAbs }}
  12. {{- with resources.GetRemote $u.String }}
  13. {{- with .Err }}
  14. {{- errorf "Unable to get remote image %s: %s. See %s" $u.String . $path }}
  15. {{- else }}
  16. {{- /* Image is a global resource (remote). */}}
  17. {{- $r = . }}
  18. {{- end }}
  19. {{- else }}
  20. {{- errorf "Unable to get remote image %s. See %s" $u.String $path }}
  21. {{- end }}
  22. {{- else }}
  23. {{- with .Page.Resources.Get $u.Path }}
  24. {{- /* Image is a page resource. */}}
  25. {{- $r = . }}
  26. {{- else }}
  27. {{- with resources.Get $u.Path }}
  28. {{- /* Image is a global resource (local). */}}
  29. {{- $r = . }}
  30. {{- else }}
  31. {{- errorf "Unable to get image %s. See %s" $u.Path $path }}
  32. {{- end }}
  33. {{- end }}
  34. {{- end }}
  35. {{- if $r }}
  36. {{- /* Set image element attributes. */}}
  37. {{- $attributes := dict "src" $r.RelPermalink "loading" "lazy" "width" (string $r.Width) "height" (string $r.Height) }}
  38. {{- with .PlainText }}
  39. {{- $attributes = merge $attributes (dict "alt" .) }}
  40. {{- end }}
  41. {{- with .Title }}
  42. {{- $attributes = merge $attributes (dict "title" .) }}
  43. {{- end }}
  44. {{- /* Render image element. */ -}}
  45. {{if .IsBlock -}}
  46. <figure {{ with .Attributes.class }} class="{{ . }}" {{ end }}>
  47. <img {{- range $k, $v :=$attributes }} {{- printf " %s=%q" $k $v | safeHTMLAttr }} {{- end -}}>
  48. {{ with .Title -}}
  49. <figcaption>
  50. <p>{{ . }}</p>
  51. </figcaption>
  52. {{- end }}
  53. </figure>
  54. {{ else }}
  55. <img {{- range $k, $v :=$attributes }} {{- printf " %s=%q" $k $v | safeHTMLAttr }} {{- end -}}>
  56. {{ end }}
  57. {{- end -}}