Generating RSS/Atom Feeds for Individual Tags in a Middleman Blog
My blog can cover a wide range of topics which I organise using tags. How can I provide filtered RSS/Atom feeds so that my readers can subscribe to the tags that interests them?
I recently needed to provide tag filtered feeds for the Unruly Tech site which uses it to aggregate relevant posts from my blog with other blog-enabled developers at Unruly. Previously Tumblr provided this functionality but it was lost when I migrated to Middleman and static hosting.
It's pretty straight forward to do once you get your head around some of the template trickery that Middleman allows and boils down to two steps.
1. Create a
Here's an example
feed.xml.builder which is essentially a simpler version of my own Atom feed. It's using the Builder templating engine (hence
.builder) to generate XML so you'll need to add
gem "builder" to your project's
Gemfile and rerun
This is essentially checking if a
tag_name local variable has been passed into the template. If so it only shows posts under that tag using Middleman's
blog.tag hash which maps tag names to an array of
A few other things to note in the template:
- Prevent Middleman from wrapping the template in any layouts you might have defined.
- Used to conveniently concatenate the tag name to relevent text when it's available.
- Used to separate the feed from where it's hosted as some readers rely on absolute URLs to find resources.
You should customise the feed layout to suite your needs. Checkout out Atom's format to see what information you can provide. You can also use the RSS format, though the template will be completely different. There should be some example templates on GitHub/Google.
2. Generate Feeds for each Tag
Now that we have the feed template, we can wire it up with our blog's tags via the
Here we're going through every tag and using Middleman's
proxy method to pass in
tag_name and generate a feed using our template.
Pretty self explanatory. This needs to be in the
ready block in the
config.rb so that Middleman Blog has ran and populated
feed.xml.builder is in our project's
sources, Middleman will automaticallt generate a
feed.xml without a
tag_name passed which will generate an unfiltered feed as we defined in our if-else block in the template.
3. You're done!
bundle exec middleman build and you'll see your feeds built and ready to go. Run them through some feed readers like Firefox's built-in one, Feedly, W3C's Validator and others to make sure everything's valid.