recently I wrote *several* sites using [Eleventy](https://www.11ty.dev/){target="_blank" rel="external"} (4? 5?). Including, over the past few days, rewriting this one! That's right, if you're reading this, we're now running on 11ty and hosted by [heckin.technology](https://heckin.technology/){target="_blank" rel="external"}. See ya, GitHub. Won't miss ya.
I've compiled some of the things I've learned in a standalone site: [11ty Lessons](https://inherentlee.codeberg.page/lessons/){target="_blank" rel="external"}.
however, since I don't know how much I'll focus on that specific site - it is mostly a sample - I am re-publishing the most useful information here. I'll skip the intro to Markdown content. I'm also going to update them where I've learned more or to better match what's represented on this site.
this will comprise of 4 parts: [related posts](#related-posts), [featured images](#featured-images), [pagination](#pagination), and [tag image preview](#tag-image-preview). Feel free to jump ahead, as none depend on the others.
---
## related posts
by default, the [Eleventy base blog](github.com/11ty/eleventy-base-blog){target="_blank" rel="external"} comes with pagination between posts. Post 2 can take you to posts 1 and 3, etc.
while that is useful for *this* site, when building another site I wanted to see a couple randomly-suggested posts that shared 1 or more tags.
I started by referring to [this GitHub issue about related posts](https://github.com/11ty/eleventy/discussions/2534){target="_blank rel="external"}. I had to fix a few errors that arose from the suggested code.
I used this in my post layout. `filterTagList` comes with the base blog by default, and removes the tags "posts" and "all." `head` also comes with the base blog. `postlist.njk` is my modified-from-the-base-blog post layout.
> this one's been edited from the lessons site. I've learned a bit more about 11ty images and feel more comfortable with my build now.
images in 11ty use the [Image Transform Plugin](https://www.11ty.dev/docs/plugins/image/#eleventy-transform){target="_blank" rel="external"}. I found it hard to find anything to reference while building this - a lot of sites in the template gallery are either text-heavy or not using the plugin - so I'm reproducing what I've got here for reference.
for any given post, my front matter references the image in this manner:
```
---
image:
src: sample-0.jpg
alt: moss on a fencepost
---
```
### image HTML transform
As mentioned, there's a plugin for images. If you started with the base blog, in `eleventy.config.js`, you'll probably find a chunk of code similar to this already in place:
// e.g. <imgloadingdecoding> assigned on the HTML tag will override these values.
loading: "lazy",
decoding: "async",
}
},
sharpOptions: {
animated: true,
},
});
```
setting `formats` to "auto" helps - use whatever type of image you want, get that type out. The default settings that came with the Eleventy base blog didn't set a `width`, which I wanted (by default, images off my camera - like the hellebore featured image for this post - are almost 5k pixels wide). I also found it helpful to set `failOnError` to true for a little more feedback.
> NOTE: It sure seems like Eleventy will fail your image processing if there's no alt text. While admirable, it would be nice if I could find any documentation for this!
### passthrough copy
as I worked through this, I thought I needed a passthrough copy for a while. You don't - just let the plugin do its thing.
### templating
I needed images to show up in 3 places:
- In the lists of posts on the home page, I wanted each post to show its featured image
- In the "related posts" section on each individual post, I wanted each related post to show its featured image
- And of course, I wanted the post to show its own featured image
### home page and related posts
both of these sections use the same template, which in my setup is called `postlist.njk`. Within the relevant links, I added the following:
[Post pagination in Eleventy is pretty straightforward](https://www.11ty.dev/docs/pagination/){target="_blank" rel="external"}, mostly requiring some specific front matter.
The home page pagination I have set up here looks like the following (in `index.njk`):
```
---
pagination:
data: collections.posts
size: 13
alias: posts
reverse: true
---
```
6 posts per page, paginate data from `collections.posts` which we'll call just `posts` for short, and do it in reverse (aka, most recent posts show first).
[You'll also likely want previous and next buttons](https://www.11ty.dev/docs/pagination/nav){target="_blank" rel="external"}. I did. Here's what I have...
#### pagination.njk
There's two components to this, the bigger one being this `pagination.njk` template. Look, I like my little icons, ok? It takes an `olderHref` and a `newerHref`, and optionally an `olderTitle` and `newerTitle`.
{% raw %}
```html
{% if olderHref or newerHref %}
<navaria-label="pagination">
<olclass="pagination {% if inPost %}post-pagination{% endif %}">
From `index.njk` we can `include "pagination.njk"`:
{% raw %}
```
{# idk why these are backwards either #}
{% set newerHref = pagination.href.previous %}
{% set olderHref = pagination.href.next %}
{% include "pagination.njk" %}
```
{% endraw %}
Yup. The order of previous vs. next is totally unintuitive to me, too.
### complex pagination
however, there's a catch. [Tag pages are *created* via pagination](https://www.11ty.dev/docs/quicktips/tag-pages/){target="_blank" rel="external"}! It's a lot harder to paginate those - you can't just use the front matter to set it up.
I untangled [this GitHub issue about double-layered pagination](https://github.com/11ty/eleventy/issues/332#issuecomment-445236776){target="_blank" rel="external"} and came to the following solution...
#### eleventy.config.js
in `eleventy.config.js`:
```js
// note that this uses the lodash.chunk method, so you’ll have to import that
note the pagination checking `tag.pageNumber` against `tag.PageSize` - the [original suggested solution](https://github.com/11ty/eleventy/issues/332#issuecomment-445236776){target="_blank" rel="external"} in the GitHub post creates an issue where the pagination loops through *all* of the tag pages bit-by-bit. This sorts that - hat tip to [TheReyzar who mentioned the issue and showed part of their solution](https://github.com/11ty/eleventy/issues/332#issuecomment-1248185406){target="_blank" rel="external"}.
#### filters.js (again)
finally, in my `filters.js` file, I add the `tagPagination` tag to the tags that get filtered using `filterTagList`:
```js
eleventyConfig.addFilter("filterTagList", function filterTagList(tags) {
today I tackled making the tag page more visually interesting.
### preview the first featured image
first, I worked on previewing the first featured image. The focus here is on digging into `collections[tag]` (reversed!) to get to the data of the first post.
{% raw %}
```html
<ulclass="taglist">
{% for tag in collections | getKeys | filterTagList | sortAlphabetically %}
{% set tagUrl %}/tags/{{ tag | slugify }}/{% endset %}
I found that having just the first featured image made the tag page hard to differentiate from any of the pages listing individual posts, so from there I moved to showing 4 images (or empty rectangles where there weren't four to show).
{% raw %}
```html
<ulclass="taglist">
{% for tag in collections | getKeys | filterTagList | sortAlphabetically %}
{% set tagUrl %}/tags/{{ tag | slugify }}/{% endset %}
with a bit of `display: grid`, we're good to go, right?
#### handling multiple aspect ratios
this had worked so far because the photos on the lessons site are from my camera in landscape mode, producing neat, identical 3:2 aspect ratios. Let's throw a wrench in that and introduce a portrait-mode photo.
thankfully, the CSS property `aspect-ratio` makes this pretty straightforward, and `object-fit` finishes the job.
```css
.taglist-link img {
aspect-ratio: 3 / 2;
object-fit: cover;
}
```
(I also set the `missing-img``<div>`s to have the same aspect ratio.)
---
There's the good stuff from [11ty Lessons](https://inherentlee.codeberg.page/lessons/){target="_blank" rel="external"}. Hope you enjoyed.