Files
HugoBlox-kit/layouts/shortcodes/gallery.html
T
George Cushen d9ff93a1c2 feat: make media library dir configurable
Move default media library from static/img/ to static/media/ as it can now contain audio, video, and other kinds of media in addition to images.

BREAKING CHANGE:
Either rename your site's static/img/ to static/media/ (and update any references to this folder in your Markdown files);

Or, add `media_dir = "img"` to `params.toml` to continue using the legacy location.
2020-07-29 18:54:28 +01:00

58 lines
2.4 KiB
HTML

{{/* Get album folder or default to `gallery/`. */}}
{{ $album := "" }}
{{ with .Get "album" }}{{ $album = . }}{{else}}{{ $album = "gallery" }}{{end}}
{{/* Set image path and page bundle that images are associated with. */}}
{{ $album_path := "" }}
{{ $resource_page := "" }}
{{ if eq .Page.Parent.Type "widget_page" }}
{{ $album_path = printf "%s/%s/*" (path.Base (path.Split .Page.Path).Dir) $album }}
{{ $resource_page = $.Page.Parent }}
{{ else }}
{{ $album_path = printf "%s/*" $album }}
{{ $resource_page = $.Page }}
{{ end }}
<div class="gallery">
{{/* Attempt to automatically load gallery images from page bundle */}}
{{ $images := ($resource_page.Resources.ByType "image").Match $album_path }}
{{ with $images }}
{{ range $images }}
{{ $image := .Resize "x190" }}
{{/* Check if the user set a caption for this image */}}
{{ $filename := path.Base .Name }}
{{ $caption := "" }}
{{ if $.Page.Params.gallery_item }}
{{ range (where (where $.Page.Params.gallery_item "album" $album) "image" $filename) }}
{{ $caption = .caption }}
{{ end }}
{{ end }}
<a data-fancybox="gallery-{{$album}}" href="{{ .RelPermalink }}" {{ with $caption }}data-caption="{{.|markdownify|emojify|safeHTMLAttr}}"{{ end }}>
<img data-src="{{ $image.RelPermalink }}" class="lazyload" alt="" width="{{ $image.Width }}" height="{{ $image.Height }}">{{/* Width & height (or low res src) required for lazy loading. */}}
</a>
{{end}}
{{else}}
{{/* Load gallery images from the `static/media/` media library or internet */}}
{{ if $.Page.Params.gallery_item }}
{{ range (where $.Page.Params.gallery_item "album" $album) }}
{{/* Set image path. */}}
{{ $.Scratch.Set "src" .image }}
{{ if gt (len .image) 4 }}
{{ if ne "http" (slicestr .image 0 4) }}
{{ $media_dir := $.Scratch.Get "media_dir" }}
{{ $.Scratch.Set "src" (printf "%s/%s" $media_dir .image | relURL) }}
{{ end }}
{{ end }}
{{/* Don't lazy load image as cannot init image size from non-Hugo asset, resulting in inaccurate anchor scrolling & active link highlighting. */}}
<a data-fancybox="gallery{{ with .album }}-{{.}}{{ end }}" {{ with .caption }}data-caption="{{.|markdownify|emojify|safeHTMLAttr}}"{{ end }} href="{{$.Scratch.Get "src"}}">
<img src="{{$.Scratch.Get "src"}}" alt="">
</a>
{{end}}
{{else}}
{{ errorf "Unable to load gallery in %s." .Page.File.Filename }}
{{end}}
{{end}}
</div>