illustration of Jordan Kohl

Filtering tags within Eleventy.js collections

- Eleventy, JavaScript, development — 1 min read

When you create a collection with Eleventy, you start with a tag. For my blog posts, I chose the tag "posts". The problem with this is that whenever I want to display all the tags for a post, it also includes "posts", which I don't like. This is a bit of code architecture that I didn’t want to expose to my readers.

Probably I could do something globally to alter the tag list in my collection wherever they are used, but I like creating re-usable functions. So instead I created a simple array filter than can be used within a template to exclude any tag. It requires a bit of repetition (wherever post tags are used), but it’s only a few characters and the flexibility means I can filter out other tags (or any string in an array) when needed. Plus the code is really simple. Here's what it looks like in my config.

eleventyConfig.addFilter("exclude", (collection, stringToFilter) => {
if (!stringToFilter) {
return collection
return (collection ?? []).filter(item => item !== stringToFilter)

Then, here is how it can be used in a template (this is within a for loop of posts):

{% set tags = | exclude('posts') %}