Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Displaying external RSS feed on page

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1315 Views
  • ramshackle
    Avatar
    Community Member
    22 Posts

    Displaying external RSS feed on page Link to this post

    Hi there,

    I am trying to display one external RSS feed on a site homepage. I have looked around a fair bit and thread http://silverstripe.org/customising-the-cms/show/11548 is a convoluted way of doing it, with formatting problems and the "=" in links in the feed text become "%" when you click through so they don't work.

    So is there an easier way of doing this? I just want to display one external RSS feed in a small window on the home page.

    Cheers

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Displaying external RSS feed on page Link to this post

    I am sure there is a better way, but I've used simplepie for this in the past...

    <?php
    class RSSJobsPage extends Page {}
    class RSSJobsPage_Controller extends Page_Controller {
       function RSSJobsPage(){
          $output = new DataObjectSet();
          $feed = new SimplePie('http://www.silverstripe.org/blog/rss', TEMP_FOLDER);
          $feed->init();
          if($items = $feed->get_items(0, 10)){
             foreach($items as $item){
                // Cast the Date
                $date = new Date('Date');
                $date->setValue($item->get_date());

                // Cast the Title
                $title = new Text('Title');
                $title->setValue($item->get_title());

                // Cast the description and strip
                $desc = new Text('Description');
                $desc->setValue(strip_tags($item->get_description()));
                $output->push(new ArrayData(array(
                   'Title' => $title,
                   'Date' => $date,
                   'Link' => $item->get_link(),
                   'Description' => $desc
                )));
             }
          }

          return $output;
       }
    }

    attached is the source code for simple pie....

    Attached Files
  • ramshackle
    Avatar
    Community Member
    22 Posts

    Re: Displaying external RSS feed on page Link to this post

    Thanks Swaiba, sorry for the delay.

    I'm pretty new to php and the SS setup I'm afraid - would I put this code as a php file in mysite/code? I'd like the external feed to appear on the homepage, so it would be called in that template - how would I call the feed from the homepage.ss template?

    Also where would I need to put the SimplePie php file?

    Cheers

  • edski
    Avatar
    Community Member
    12 Posts

    Re: Displaying external RSS feed on page Link to this post

    I'm not sure the advantage of using the SimplePie.php file. I found that if I add in the following line to the code example it works:

    include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/simplepie.inc'));

    <?php
    class RSSJobsPage extends Page {}
    class RSSJobsPage_Controller extends Page_Controller {
    function RSSJobsPage(){

    include_once(Director::getAbsFile(SAPPHIRE_DIR . '/thirdparty/simplepie/simplepie.inc'));

    $output = new DataObjectSet();
    $feed = new SimplePie('http://www.silverstripe.org/blog/rss', TEMP_FOLDER);
    $feed->init();
    if($items = $feed->get_items(0, 10)){
    foreach($items as $item){
    // Cast the Date
    $date = new Date('Date');
    $date->setValue($item->get_date());

    // Cast the Title
    $title = new Text('Title');
    $title->setValue($item->get_title());

    // Cast the description and strip
    $desc = new Text('Description');
    $desc->setValue(strip_tags($item->get_description()));
    $output->push(new ArrayData(array(
    'Title' => $title,
    'Date' => $date,
    'Link' => $item->get_link(),
    'Description' => $desc
    )));
    }
    }

    return $output;
    }
    }

    1315 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.