0

我目前有这段代码,它从我的博客中提取了第一条记录:

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

这很好用,但是我需要使用的不仅仅是第一篇博客文章,有没有办法可以遍历所有文章?

4

1 回答 1

0

最后只是把它放在我的观点中:

- @rss = SimpleRSS.parse open('http://blog.example.co.uk/feed/')
%p
  Latest blog articles:

  %ul.about
    - @rss.items.first(2).each do |i|
      %li.blog
        %a{:href => "http://blog.example.co.uk/#{i.link}"} #{if i.title.length > 80 then "#{ i.title[0..65]} ..." else "#{i.title}" end}
于 2013-07-22T16:26:16.580 回答