Creating an RSS/XML feed

How can I create an rss feed for my blog in Middleman?

I think you can find useful code here.

http://recipes.sinatrarb.com/p/views/rss_feed_with_builder

Needs to be adapted to Middleman though.

Interesting, thanks. Does placing this code in a HAML view make sense?

You can do it with a .builder extension, or you can just write normal Haml.

Check out middleman-blog’s default feed.xml template: https://github.com/middleman/middleman-blog/blob/master/lib/middleman-blog/template/source/feed.xml.builder

Nayefc, did you have any success with implementing this?

Yep, worked beautifully.

Would you mind sharing the code. That would save some work for me.

Sure. I created a feed.xml.builder file in my /source:

1  xml.instruct!
2  xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
3    xml.title "Nayef Copty"
4    xml.subtitle "Blog & Portfolio"
5    xml.id "http://nayefcopty.com/blog"
6    xml.link "href" => "http://nayefcopty.com/blog"
7    xml.link "href" => "http://blog.url.com/feed.xml", "rel" => "self"
8    xml.updated blog.articles.first.date.to_time.iso8601
9    xml.author { xml.name "Nayef Copty" }
10 
11   blog.articles[0..10].each do |article|
12     xml.entry do
13       xml.title article.title
14       xml.link "rel" => "alternate", "href" => article.url
15       xml.id article.url
16       xml.published article.date.to_time.iso8601
17       xml.updated article.date.to_time.iso8601
18       xml.author { xml.name "Nayef Copty" }
19       xml.summary article.summary, "type" => "html"
20       xml.content article.body, "type" => "html"
21     end
22   end
23 end

Then in your config:

page "/feed.xml", :layout => false
1 Like

Hey nayefc,
I took your example and put it in my feed.xml.builder and my config.rb. I changed everything so it fits for me except line 7. I don’t know what to put in there. Could you please help me out with this? And, I know this is a kind of weird question but I’m really knew to this all and so I have to ask. What to do next? There is no RSS feed button or link or something. Please can you help me with this?
Thanks a lot!!!

Did you create a feed.xml.builder file? If so, and if you added the config line I have posted, all you need to do then is access the URL that you posted in line 7 above. Probably: www.yourwebsite.com/feed.xml

The code above just generates your XML feed page. You will then need to link to it (www.yourwebsite.com/feed.xml) in one of your views (HTML files).

My HAML to link to my RSS feed looks like this:

%a{:href => "/feed.xml", :target => "_blank"}
  %img.img-left{:alt => "rss", :src => "/img/rss.png"}/

You can find my rss.png image at www.nayefcopty.com.

Makes sense?

Absolutely. Thanks a lot. I’ll try it as soon as I have the time to and tell how it worked. Thanks again!

Works perfect. Now I just need to figure how to build this site so that it looks like the rest of my site :slight_smile:
I tried to build it just the normal way but then I got a lot of errors because of the stuff the layout.erb added to the site. But it will work for now.
Thanks again!

What do you mean? It should be an XML page with no layout at all.

Yepp, my wrong :smiley: Works absolutely as it should.

I would like to add a small contribution because I had a problem compiling the rss feed: keep in mind that you need to have the builder gem installed.