gallery.html 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 := .Name }}
  22. {{ $caption := "" }}
  23. {{ if $.Page.Params.gallery_item }}
  24. {{ range (where $.Page.Params.gallery_item "album" $album) }}
  25. {{if eq (printf "%s/%s" $album .image) $filename}}{{ with .caption }}{{ $caption = . }}{{end}}{{end}}
  26. {{ end }}
  27. {{ end }}
  28. <a data-fancybox="gallery-{{$album}}" href="{{ .RelPermalink }}" {{ with $caption }}data-caption="{{.}}"{{ end }}>
  29. <img src="{{ $image.RelPermalink }}" alt="">
  30. </a>
  31. {{end}}
  32. {{else}}
  33. {{/* Load gallery images from the `static/img/` 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. {{ $.Scratch.Set "src" (printf "img/%s" .image | relURL) }}
  41. {{ end }}
  42. {{ end }}
  43. <a data-fancybox="gallery{{ with .album }}-{{.}}{{ end }}" {{ with .caption }}data-caption="{{.}}"{{ end }} href="{{$.Scratch.Get "src"}}">
  44. <img src="{{$.Scratch.Get "src"}}" alt="">
  45. </a>
  46. {{end}}
  47. {{else}}
  48. {{ errorf "Unable to load gallery in %s." .Page.Filename }}
  49. {{end}}
  50. {{end}}
  51. </div>