Quick Tip #004—Zero Maintenance Tag Pages for your Blog
This quick tip will show you how to automatically generate Tag Pages (lists of content tagged into a collection).
We’ll use pagination to automatically generate a template for each tag we want to link to.
Here’s a sample pagination template using Nunjucks:
---
pagination:
data: collections
size: 1
alias: tag
permalink: /tags/{{ tag }}/
---
<h1>Tagged “{{ tag }}”</h1>
<ol>
{% set taglist = collections[ tag ] %}
{% for post in taglist | reverse %}
<li><a href="{{ post.url }}">{{ post.data.title }}</a></li>
{% endfor %}
</ol>
First up notice how we’re pointing our pagination
to iterate over collections
, which is an object keyed with tag names pointing to the collection of content containing that tag.
Consider these two sample markdown posts, one using a tech
tag and one using a personal
tag:
---
title: My First Post
tags:
- tech
---
---
title: My Second Post
tags:
- personal
---
Our pagination template above will now generate two pages: /tags/personal/index.html
and /tags/tech/index.html
.
The great thing here is that we don’t have to manage our tag list in a central place. These tags can be littered throughout our content and individual tag pages will be created automatically.
Filtering / Excluding Jump to heading
Have a tag you’d like to exclude from this list? Use pagination filtering like this:
---
pagination:
data: collections
size: 1
alias: tag
filter:
- tech
permalink: /tags/{{ tag }}/
---
Now Eleventy will only generate a /tags/personal/
template and tech
will be ignored.
In Practice Jump to heading
This is currently in use on the eleventy-base-blog
sample project. Check out source code in the tags.njk
template and see a live demo.
All Quick Tips
#001
—Inline Minified CSS#002
—Inline Minified JavaScript#003
—Add Edit on GitHub Links to All Pages#004
—Zero Maintenance Tag Pages for your Blog#005
—Super Simple CSS Concatenation#006
—Adding a 404 Not Found Page to your Static Site#007
—Fetch GitHub Stargazers Count (and More) at Build Time#008
—Trigger a Netlify Build Every Day with IFTTT#009
—Cache Data Requests#010
—Transform Global Data using an `eleventyComputed.js` Global Data File#011
—Draft Posts using Computed Data- View all of the Eleventy Quick Tips.