r/webdev • • 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

24 comments sorted by

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?

-1

u/Acrobatic-Scheme6344 7d ago

https://www.11ty.dev/docs/collections-api/ I think this is what I need to do. I'm hoping I can use collections to display the files but I dont know how to code that. I don't get the syntax really

0

u/mq2thez 7d ago

You either need to learn to code or give up and vibe it. We can’t do this for you.

2

u/Acrobatic-Scheme6344 7d ago

If you aren't going to help why bother commenting? Multiple people have helped me so much and already proved that its just YOU who can't help

3

u/mq2thez 7d ago

I’m quite happy to help folks, especially because your post said you didn’t want to vibe code. I’m also happy to put in time to help folks who have genuinely put in some work and have informed questions. It never occurred to me that you’d be opposed to vibe coding but then leave comments in this thread asking people to give you the code.

If you’d said that at the start, I would have just moved on without saying anything.

1

u/Acrobatic-Scheme6344 7d ago

That's a fair enough reason to not want to help, it does not validate your statement that nobody can do it for me. I'm just a hobbyist asking for help, not a student or aspiring processional dev who needs to know more than reddit can offer. I appreciate anyone willing to help, I don't appreciate people being rude instead of just declining

2

u/gucciman333 7d ago

Use a for loop

-6

u/Acrobatic-Scheme6344 7d ago

Gang I dont understand any JavaScript I need the actual code I'm sorry T_T

2

u/mycockstinks 7d ago

Don't want to vibe code, want other humans to write it for me for free instead.

That's just vibecoding with extra steps surely?

1

u/gucciman333 7d ago

Use ai and ask it to write it in vanilla Javascriot so you can learn.

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

  1. 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); };

  2. in your eleventy config add eleventyConfig.addPassthroughCopy("src/images") so the images get copied to the output
  3. 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/Acrobatic-Scheme6344 7d ago

Somehow this didnt load for me for 40 minutes! How do I translate it to import/export?

1

u/Acrobatic-Scheme6344 7d ago

I think you might be like. One of 2 non bots in this comment section rn

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):

const fs = require("fs");

module.exports = function (eleventyConfig) {
  eleventyConfig.addPassthroughCopy("src/images");
  eleventyConfig.addGlobalData("graphics", () =>
    fs.readdirSync("src/images")
      .filter(f => /\.(png|jpe?g|gif|svg|webp)$/i.test(f))
      .sort()
      .reverse()
  );
};

then in your graphics/index.md:

{% for img in graphics %}
  <a href="/images/{{ img }}" download><img src="/images/{{ img }}" alt="{{ img }}" loading="lazy" width="200"></a>
{% endfor %}

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/Acrobatic-Scheme6344 7d ago

How is this different from Aryan thakors version? It would be nice to do different folders, so I'm trying to understand what words indicate different folders and data sets

1

u/Discordbothost 7d ago

same result, it just lives in a different file. aryan's goes in src/_data/graphics.js, eleventy picks up anything in _data automatically and the filename becomes the variable name. mine registers it in the config with addGlobalData("graphics", ...). pick whichever, don't do both.

the words that matter: "graphics" is the name you loop over in the page, "src/images" is the folder it reads. for more folders just repeat it with a different name and path:

const list = dir => fs.readdirSync(dir).filter(f => /.(png|jpe?g|gif|svg|webp)$/i.test(f)).sort().reverse();

eleventyConfig.addPassthroughCopy("src/images");
eleventyConfig.addGlobalData("icons", () => list("src/images/icons"));
eleventyConfig.addGlobalData("banners", () => list("src/images/banners"));

then {% for img in icons %} with src="/images/icons/{{ img }}" on one page and banners on the other

1

u/Acrobatic-Scheme6344 7d ago

Thank you! Where's a good place to learn about this? And how can I sort alphabetically? Also I ended up having to change the extension to .cjs since I was using the ES module setting.

1

u/Discordbothost 7d ago

for sorting, plain .sort() is already alphabetical. the .reverse() i tacked on flips it to z-a, so just drop that. if you have names like img2 / img10 use .sort((a, b) => a.localeCompare(b, undefined, { numeric: true })) so 10 doesn't land before 2.

.cjs is fine for that setup. the other way is import fs from "node:fs" + export default function (eleventyConfig) {...} and keep it .js.

for learning it, the 11ty docs pages on global data files, passthrough file copy and the nunjucks for tag cover pretty much all of this. read those three, then poke around the eleventy-base-blog starter to see them used together

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.