Getting up to Speed with Eleventy: Config and Collections
Continuing his Eleventy tutorial, David Eastman shows how to configure the system, make use of templates, what collections are, and more.
Jan 20th, 2024 3:00am by
Photo by John Cameron on Unsplash.

How we left things
module.exports = function(eleventyConfig) {
// Return your Object options:
return {
dir: {
input: "src",
output: "public"
}
}
};
As we are changing configuration, we need to restart the server. And when we do, nothing will happen until we actually move our source material appropriately and delete the old site. That incudes _includes.
When we make the necessary file moves and run, our kitten (ok, a different kitten) returns. The directory structure, ignoring the modules directory but including the config file, now looks like this:
If you are reading this from our website, you’ll see a nice little picture of me at the end, in a round bubble. I’d like to put that on my site, to the left of the header. Reverse engineering, I can see that my image is wrapped in a “post-author-avatar” class. Like most hackers, I learn by
.author-profile-photo-column img
{
display: block;
width: 100%;
aspect-ratio: 1/1;
-o-object-fit: cover;
object-fit: cover;
border-radius: 30px
}
..
header {
background-color: #333;
color: white;
padding: 20px;
h1 {
display: inline-block;
vertical-align: middle;
}
img {
width: 5%;
border-radius: 30px;
display: inline-block;
vertical-align: middle;
float: left;
}
}
..
module.exports = function(eleventyConfig) {
eleventyConfig.addPassthroughCopy('./src/*.css');
eleventyConfig.addPassthroughCopy('./src/*.png');
return {
dir: {
input: "src",
output: "public"
}
}
};

The cat is back
As a reminder, the index page is produced with index.md:
---
layout: "layout.html"
title: "David's Home Page"
---
Hi there, I like cats!!
Now it should be pointed out that I don’t like cats that much, but I have started this theme so I am condemned to complete it. So I am going to pretend I want to add pages about famous cats.
Our steel thread is that we want to write content in Markdown, and let Eleventy take care of creating the site. While maintaining the site, we don’t want to deal with HTML.
So we’ll make a cats directory in the src, and create our first cat garfield.md into that:
---
layout: "layout.html"
title: "Garfield"
---
Garfield doesn't like Mondays.
If you have the server going (I have it in another Warp tab) you will see Eleventy processing it behind the scenes.
And indeed if we follow the browser path created, we see the following:

That isn’t Garfield
---
layout: "layout.html"
title: "David's Home Page"
catimage: "https://placekitten.com/800/400"
---
Hi there, I like cats!!
And alter our layout to use the new catimage variable:
..
<body>
<header>
<img src="photo-of-me.png">
<h1>{{ title }}</h1>
</header>
<img class="cat" src="{{ catimage }}" alt="A nice cat"> {{content}}
<footer>
<p>© 2024 Nice Cat Page. All rights reserved.</p>
</footer>
</body>
..
---
layout: "layout.html"
title: "Garfield"
catimage: "Garfieldand_friends.png"
---
Garfield doesn't like Mondays.
Almost done. But it won’t work yet. Look at the directory:
My garfield image didn’t make it into the output public directory. And we know why, from our config above. So lets adjust the config file so cats go straight through too:
module.exports = function(eleventyConfig) {
// Return your Object options:
eleventyConfig.addPassthroughCopy('./src/*.css');
eleventyConfig.addPassthroughCopy('./src/*.png');
eleventyConfig.addPassthroughCopy('./src/cats/*.png');
return {
dir: {
input: "src",
output: "public"
}
}
};
Well, actually the image is one directory below, isn’t it? So one more alteration to the Garfield file:
---
layout: "layout.html"
title: "Garfield"
catimage: "../Garfieldand_friends.png"
---
Garfield doesn't like Mondays.
And finally we get our cat:
We’ve also, using the same logic, solved our avatar problem. It always sits in the same place ready for every page to share it, but not the same place relative to the calling page. By putting a slash in front or it, we state that we are looking for it in the root:
..
<header>
<img src="/photo-of-me.png">
<h1>{{ title }}</h1>
</header>
..
Collections and Tags
Fortunately, Eleventy has this covered. It has the concept of a collection of pages. And we use tags in the front matter to mark a page as being part of the collection:---
layout: "layout.html"
title: "Garfield"
catimage: "../Garfieldand_friends.png"
tags: cats
---
Garfield doesn't like Mondays.
We can then loop through our cats and make some form of index:
..
<div class="listcontainer">
<ul>
{% for cat in collections.cats %}
<li>
<a href="/cats/{{ cat.data.title}}">{{ cat.data.title }} </a>
</li>
{% endfor %}
</ul>
</div>
..

List of cats
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.