Creating Multiple Feeds with the 11ty RSS Plugin
Creating a feed using the 11ty RSS plugin is pretty straightforward and doesn’t require much configuration. Less obvious to me was how to create a second feed for a filtered subset of my data. In this case I wanted to create a feed of only posts tagged with ‘Drupal’ that can be submitted to Planet Drupal.
Since the plugin required so little configuration, it wasn’t clear to me if there was a way to make the plugin aware of a second feed template. As it turns out, the plugin is smart enough to pick up on any number of feed templates, located anywhere in your codebase. I created a /src/_feeds
directory that contained by default feeds.njk
template and my new Drupal-specific drupal.njk
template. As long as you add a unique permalink in this template’s json frontmatter, the plugin will generate a new feed for you. In this case I used:
"permalink": "drupal.xml",
Next was determining how to filter my 11ty collection based on a specific tag. This quick tip on creating tag pages gave me what I needed. Instead of iterating through collections.all
as I did in my default feed, I added the following tag filter:
{`%- for post in collections[ 'drupal' ] | reverse %`}
As a result, I had only my content tagged with Drupal. The resulting drupal.njk
feed template looked like this:
---json { "permalink": "drupal.xml", "eleventyExcludeFromCollections": true, "metadata": { "title": "Brian Perry", "subtitle": "A collection of my Drupal related posts", "url": "https://brianperry.dev/", "feedUrl": "https://brianperry.dev/feed.xml", "author": { "name": "Brian Perry", "email": "heybrianperry@gmail.com" } } } --- <?xml version="1.0" encoding="utf-8"?> <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:base="{{ metadata.url }}"> <channel> <title>{{ metadata.title }}</title> <link>{{ metadata.url }}</link> <description>{{ metadata.subtitle }}</description> <language>en</language> {%- for post in collections[ 'drupal' ] | reverse %} {% set absolutePostUrl %}{{ post.url | url | absoluteUrl(metadata.url) }}{% endset %} <item> <title>{{ post.data.title }}</title> <link>{{ absolutePostUrl }}</link> <description>{{ post.data.feed_excerpt | htmlToAbsoluteUrls(absolutePostUrl) | feedEncode }}</description> <pubDate>{{ post.date | dateToRfc822 }}</pubDate> <dc:creator>{{ metadata.author.name }}</dc:creator> <guid>{{ absolutePostUrl }}</guid> </item> {%- endfor %} </channel> </rss>
Now all I have to do is write some Drupal related posts…