- edit data/carousel.yaml file
- https://hugocodex.org/add-ons/slider-carousel/
Yes, you can dynamically load images from a folder (e.g., uploads) in Hugo without manually editing the yaml file every time. Hugo provides powerful features like Page Resources and Hugo Pipes that allow you to work with files dynamically. Here's how you can achieve this:
Place all your images in a specific folder, e.g., static/uploads/. Hugo's static folder is ideal for files that don't need processing (like images).
your-hugo-project/
├── static/
│ └── uploads/
│ ├── image1.jpg
│ ├── image2.jpg
│ └── image3.jpg
├── content/
├── layouts/
└── config.yaml
Hugo has a built-in function called readDir that allows you to read files from a directory dynamically. You can use this to list all images in the uploads folder.
In your layouts/ directory, create a partial or template to dynamically load the images. For example:
<!-- layouts/partials/carousel.html -->
<div class="carousel">
{{ $uploads := "uploads" }} <!-- Path to the uploads folder -->
{{ range readDir (printf "static/%s" $uploads) }}
{{ if in .Name ".jpg" ".jpeg" ".png" ".gif" }} <!-- Filter for image files -->
<div class="carousel-item">
<img src="/{{ $uploads }}/{{ .Name }}" alt="{{ .Name }}">
</div>
{{ end }}
{{ end }}
</div>Include the partial in your page template (e.g., layouts/_default/single.html):
{{ partial "carousel.html" . }}If you want to associate metadata (like content_html) with each image dynamically, you can use a data file (e.g., data/images.yaml) or front matter.
# data/images.yaml
images:
- image: /uploads/image1.jpg
content_html: "1"
- image: /uploads/image2.jpg
content_html: "2"Then, update your partial to use this data:
<!-- layouts/partials/carousel.html -->
<div class="carousel">
{{ range .Site.Data.images }}
<div class="carousel-item">
<img src="{{ .image }}" alt="{{ .content_html }}">
<div class="content">{{ .content_html | safeHTML }}</div>
</div>
{{ end }}
</div>If you want to fully automate the process (e.g., generate the data/images.yaml file whenever new images are added), you can write a small script (e.g., in Python or Bash) to scan the uploads folder and update the yaml file.
- Dynamic: No need to manually update the
yamlfile every time you add or remove images. - Flexible: You can easily filter, sort, or process images using Hugo's templating functions.
- Scalable: Works well for large numbers of images.
If you want to associate images with specific pages or posts, consider using Page Bundles in Hugo. This allows you to store images alongside your content files and access them dynamically.
For example:
content/
└── posts/
└── my-post/
├── index.md
├── image1.jpg
└── image2.jpg
You can then access these images in your template using .Resources:
{{ range .Resources.ByType "image" }}
<img src="{{ .RelPermalink }}" alt="{{ .Name }}">
{{ end }}By using these methods, you can dynamically load images from a folder in Hugo without manually editing the yaml file every time.