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 feed.xml Template

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 bundle install.

layout: false
root_url = ""
feed_url = URI.join(root_url, current_page.path)

if defined? tag_name
  articles = blog.tags[tag_name]
  page_url = URI.join(root_url,
  articles = blog.articles
  page_url = URI.join(root_url, "#{blog.options.prefix.to_s}/")

xml.feed "xmlns" => "" do
  xml.title [tag_name, "Jahed's Blog"].compact.join(" - ")
  xml.subtitle [tag_name, "Posts by Jahed"].compact.join(" ") page_url "href" => page_url "href" => feed_url, "rel" => "self"
  xml.updated( unless articles.empty? { "Jahed Ahmed" }

  articles[0..5].each do |article|
    article_url = URI.join(root_url, article.url)
    xml.entry do
      xml.title article.title "rel" => "alternate", "href" => article_url article_url
      xml.updated { "Jahed Ahmed" }
      xml.summary article.summary(500), :type => "html"
      xml.content article.body, "type" => "html"

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 Articles.

A few other things to note in the template:

layout: false
Prevent Middleman from wrapping the template in any layouts you might have defined.
[tag_name, ...].compact.join
Used to conveniently concatenate the tag name to relevent text when it's available.
URI.join(root_url, ...)
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 config.rb.

ready do
  blog.tags.each_key do |tag_name|
    destination_url = "/tags/#{tag_name}/feed.xml"
    template_url = "/feed.xml"
    proxy destination_url, template_url, :locals => { :tag_name => tag_name }

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 blog.tags.

Also, since 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!

Atom Validation Passed!

Just run 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.