gallery.html 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. {{/* Get album folder or default to `gallery/`. */}}
  2. {{ $album := "" }}
  3. {{ with .Get "album" }}{{ $album = . }}{{else}}{{ $album = "gallery" }}{{end}}
  4. {{/* Set image path and page bundle that images are associated with. */}}
  5. {{ $album_path := "" }}
  6. {{ $resource_page := "" }}
  7. {{ if eq .Page.Parent.Type "widget_page" }}
  8. {{ $album_path = printf "%s/%s/*" (path.Base (path.Split .Page.Path).Dir) $album }}
  9. {{ $resource_page = $.Page.Parent }}
  10. {{ else }}
  11. {{ $album_path = printf "%s/*" $album }}
  12. {{ $resource_page = $.Page }}
  13. {{ end }}
  14. <div class="gallery">
  15. {{/* Attempt to automatically load gallery images from page bundle */}}
  16. {{ $images := ($resource_page.Resources.ByType "image").Match $album_path }}
  17. {{ with $images }}
  18. {{ range $images }}
  19. {{ $image := .Resize "x190" }}
  20. {{/* Check if the user set a caption for this image */}}
  21. {{ $filename := path.Base .Name }}
  22. {{ $caption := "" }}
  23. {{ if $.Page.Params.gallery_item }}
  24. {{ range (where (where $.Page.Params.gallery_item "album" $album) "image" $filename) }}
  25. {{ $caption = .caption }}
  26. {{ end }}
  27. {{ end }}
  28. <a data-fancybox="gallery-{{$album}}" href="{{ .RelPermalink }}" {{ with $caption }}data-caption="{{.|markdownify|emojify|safeHTMLAttr}}"{{ end }}>
  29. <img data-src="{{ $image.RelPermalink }}" class="lazyload" alt="" width="{{ $image.Width }}" height="{{ $image.Height }}">{{/* Width & height (or low res src) required for lazy loading. */}}
  30. </a>
  31. {{end}}
  32. {{else}}
  33. {{/* Load gallery images from the `static/media/` media library or internet */}}
  34. {{ if $.Page.Params.gallery_item }}
  35. {{ range (where $.Page.Params.gallery_item "album" $album) }}
  36. {{/* Set image path. */}}
  37. {{ $.Scratch.Set "src" .image }}
  38. {{ if gt (len .image) 4 }}
  39. {{ if ne "http" (slicestr .image 0 4) }}
  40. {{ $media_dir := site.Params.media_dir | default "media" }}
  41. {{ $.Scratch.Set "src" (printf "%s/%s" $media_dir .image | relURL) }}
  42. {{ end }}
  43. {{ end }}
  44. {{/* Don't lazy load image as cannot init image size from non-Hugo asset, resulting in inaccurate anchor scrolling & active link highlighting. */}}
  45. <a data-fancybox="gallery{{ with .album }}-{{.}}{{ end }}" {{ with .caption }}data-caption="{{.|markdownify|emojify|safeHTMLAttr}}"{{ end }} href="{{$.Scratch.Get "src"}}">
  46. <img src="{{$.Scratch.Get "src"}}" alt="">
  47. </a>
  48. {{end}}
  49. {{else}}
  50. {{ errorf "Unable to load gallery in %s." .Page.File.Filename }}
  51. {{end}}
  52. {{end}}
  53. </div>