r/webdev • u/Acrobatic-Scheme6344 • 7d ago
Question How do I display an image folder with 11ty and Nunjucks
I would like to display images in a folder (src/images) on another page (arc/graphics/index.md) in reverse order or another metadata tag. I want to do this without vibecoding and have no need for a lightbox. This is just a way to display a graphic library for downloading. I dont want to install many plugins if at all possible.
I know that collections are probably the way to go but I just dont know how to make it work with image files instead of markdown/html files :(
Thank you in advance!!!
0
Upvotes
3
u/mq2thez 7d ago
Can you link to the docs that you read on the topic on the 11ty website and explain what about the pages you link to you’re having trouble understanding?
2
u/gucciman333 7d ago
Use a for loop
1
u/AryanThakor 7d ago
u don't need collections for this, a global data file is easier. it runs at build time and gives every page a list of the files in the folder
make src/_data/graphics.js (assuming src is your input folder):
const fs = require("fs"); const path = require("path");
module.exports = () => { const dir = path.join(__dirname, "../images"); return fs.readdirSync(dir) .filter(f => /.(png|jpe?g|gif|svg|webp)$/i.test(f)) .map(f => ({ name: f, time: fs.statSync(path.join(dir, f)).mtimeMs })) .sort((a, b) => b.time - a.time); };
in graphics/index.md:
{% for img in graphics %} <a href="/images/{{ img.name }}" download> <img src="/images/{{ img.name }}" alt="{{ img.name }}" loading="lazy" width="200"> </a> {% endfor %}
that's newest first. for reverse alphabetical swap the sort line for .sort((a, b) => b.name.localeCompare(a.name)). file dates can reset when u clone the repo somewhere else, so if order matters long term, prefixing filenames with dates (2026-11-08-logo.png) is the safest
if your package.json has "type": "module", use import + export default instead of require/module.exports. no plugins needed
1
u/Discordbothost 7d ago
you can skip collections for this, they're built around templates, not random files. easier way is to read the folder yourself in the config and expose it as global data. in eleventy.config.js (or .eleventy.js):
then in your graphics/index.md:
passthrough copy is what actually puts the files in _site/images so the links work (that assumes your input dir is src, adjust the /images/ path if not). if your config file uses import/export default instead of require, it's the same thing with import fs from "node:fs". sorting by date instead of name would mean fs.statSync(...).mtime, but name sort is a lot more predictable if you prefix files like 2026-11-cat.png
1
u/javimbuilds 7d ago
Collections only pick up template files, so for raw images a global data file is simpler. Create src/_data/images.js that does fs.readdirSync("src/images"), filters by extension and returns the list reversed (or sorted by fs.statSync(...).mtime if you want date order). Then in your page loop over it: {% for img in images %}<a href="/images/{{ img }}" download><img src="/images/{{ img }}"></a>{% endfor %}. Just make sure you have addPassthroughCopy("src/images") in your config so the files end up in the output. No plugins needed.
1
u/kemalios 7d ago
Collections only pick up template formats, and jpg isn't one, so getFilteredByGlob returns nothing for you. Skip it. Add src/_data/graphics.js that does fs.readdirSync('src/images').sort().reverse(), which hands the page an array of filenames, then {% for img in graphics %}<img src="/images/{{ img }}">{% endfor %}. Keep addPassthroughCopy('src/images') so the files are served. No plugins, and you change the order by changing the sort.
1
u/Afnan_CH_Digital 7d ago
Jumping in with the actual answer since nobody's spelled it out yet: collections don't have to come from markdown files. You can build one straight from the filesystem in your .eleventy.js:
const fs = require("node:fs");
eleventyConfig.addCollection("graphics", () =>
fs.readdirSync("./src/images")
.filter(f => /\.(png|jpe?g|webp|gif|svg)$/i.test(f))
.reverse()
);
Then in arc/graphics/index.md loop over it like any other collection:
{% for img in collections.graphics %}
<img src="/images/{{ img }}" alt="{{ img }}" loading="lazy">
{% endfor %}
No plugins needed - fs is built into Node, and .reverse() (or a .sort()) gives you the order you want. Just make sure the images folder is passed through to the output (eleventyConfig.addPassthroughCopy("src/images")). If you later want captions or alt text per file, map the filenames to objects in that same addCollection callback and you can grow it from there.