我目前有这段代码,它从我的博客中提取了第一条记录:
def blogFeed
begin
rss = SimpleRSS.parse(open('http://blog.example.co.uk/feed/').read)
if rss.items.first.title.length > 80 then
title = "#{ rss.items.first.title[0..70]} ..."
else
title = rss.items.first.title
end
date = rss.items.first.pubDate.strftime("%d %B %Y")
@links = { :title => title, :url => rss.items.first.link, :date => date }
rescue OpenURI::HTTPError
@links = { :title => "", :url => "#", :date => "" }
end
return @links
end
这就是我在我看来引用它的方式:
%span.feed
%span.bold
LATEST BLOG:
- blog = blogFeed
- if blog[:title] != ""
= raw blog[:title]
%a{ :href => "#{ raw blog[:url] }" } READ MORE
- else
%a{ :href => "/blog" } CLICK HERE TO VISIT OUR BLOG
这很好用,但是我需要使用的不仅仅是第一篇博客文章,有没有办法可以遍历所有文章?