Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Thomas Sarmis
Thomas Sarmis

Posted on • Edited on • Originally published atgreekdeveloper.com

     

Building a folder-based gallery for Hugo

A custom hugo shortcode that builds an image gallery from images inside a folder

Source Code

Image Discovery & Gallery Layout

<!-- "site root"\layouts\shortcodes\foldergallery.html --><style>div.gallery{display:flex;flex-wrap:wrap;}div.gallerya{flex-grow:1;object-fit:cover;margin:2px;display:flex;}div.galleryaimg{height:200px;object-fit:cover;flex-grow:1;}</style><linkrel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.4.0/jquery.fancybox.min.css"/><divclass="gallery">        {{ $path := print "content\\" .Page.Dir (.Get "src") }}        {{ $url  := print .Page.URL (.Get "src") }}        {{ range (readDir $path)  }}                        {{ $src := print $url "/" .Name }}<adata-fancybox="gallery"href="{{ $src }}"><imgsrc="{{ $src }}"><br/></a>        {{ end }}</div>
Enter fullscreen modeExit fullscreen mode

Fancy Box

<!--1. Replace theme's jquery with 3.3.1 version2. Add the fancybox3 script--><scriptsrc="https://code.jquery.com/jquery-3.3.1.min.js"integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="crossorigin="anonymous"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.4.0/jquery.fancybox.min.js"></script>
Enter fullscreen modeExit fullscreen mode

Usage

  { {< foldergallery src="imgs" >} }
Enter fullscreen modeExit fullscreen mode

Will create an image gallery with the images in the imgs folder

Sample

Originally posted atgreek developer

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
summittdweller profile image
Mark McFate
  • Location
    Iowa
  • Work
    Digital Library Applications Developer at Grinnell College
  • Joined

This is cool. I tried building a vacation image site using this... while I was on vacation, and failed. Wondering if there is a "complete" implementation of this code somewhere in Github or a similar repository?

CollapseExpand
 
studiostark1 profile image
Michael Stark
Artdesigner, trying to look cool
  • Location
    Germany
  • Joined
• Edited on• Edited

Hi,
came from Jekyll (switched after two weeks for Hugo and never regret it).

I got your Foldergallery running. OK the DOM elements are created but the images are not loaded. Tried a little bit with the .PageURL but didn't get the URL pointing in my folder directory.

What therm do i have to search for, the Hugo documentation is a mouthful... ;) ???

CollapseExpand
 
sarmis profile image
Thomas Sarmis
System Architect / Software Engineer
  • Email
  • Location
    Athens, Greece
  • Education
    Master in Computer Science
  • Work
    Senior Software Engineer at Kaizen Gaming
  • Joined

I would suggest comparing the src attribute of dom elements (imgs) against the actual path of the images in the public folder

CollapseExpand
 
sheldonhull profile image
Sheldon
Bio on github.com/sheldonhull
  • Location
    Texas
  • Work
    Systems Development Engineer
  • Joined

That's pretty cool. I'll have to give this a shot.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

System Architect / Software Engineer
  • Location
    Athens, Greece
  • Education
    Master in Computer Science
  • Work
    Senior Software Engineer at Kaizen Gaming
  • Joined

More fromThomas Sarmis

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp