Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Yet another SimplePie Question (this time for SS3)

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

Page: 1
Go to End
Author Topic: 543 Views
  • dinResita
    Avatar
    Community Member
    3 Posts

    Yet another SimplePie Question (this time for SS3) Link to this post

    I did try to use Willr answer to topic Simple Pie with Silverstripe, however I don't know how to get the RSS Feeds in my template.

    I did following: mysite/code/RSSPage.php (Note: I did experiment also with the latest SimplePie - same result hovewer on simplepie.inc)

    <?php

    class RSSPage extends Page {
       static $description= "Pagina feed RSS";
    }

    class RSSPage_Controller extends Page_Controller {

    function My_Feeds($url) {
    // require_once("../framework/thirdparty/simplepie/simplepie.inc");
       require_once("../framework/thirdparty/simplepie2/autoloader.php");
    $this->feed = new SimplePie($url, "../silverstripe-cache/");

       $this->feed->init();
       $this->feed->handle_content_type();

    $items = new DataObjectSet();

    if($this->feed->get_items()) {
    foreach ($this->feed->get_items() as $item) {
    $items->push(new ArrayData($item));
    }
    }
    return $items;
    }

    }

    This does not display the feed:
    excerpt from templates/RSSPage.ss

    <ul>
    <% loop My_Feeds %>
          <li>$My_Feeds - $Title</li>
    <% end_loop %>
    </ul>

    My question: how can I access the DataObjectSet from a .ss file?

  • dinResita
    Avatar
    Community Member
    3 Posts

    Re: Yet another SimplePie Question (this time for SS3) Link to this post

    Finaly, I did it!

    So, first is first -> the above code is deprecated for SS3.0. You find this out if you activate development mode. Put following code in _config.php (mysite folder)

    Director::set_environment_type("dev");


    Now, call the RSSPage -> lots of deprecated things.
    The debugging documentation helped me a lot.

    Now the results:
    RSSPage.ss

    <ul>
    <ul>
       <% loop My_Feeds("http://php.net/feed.atom") %>
          <li>$Date <a href="$Link">$Title</a></li>   
       <% end_loop %>
    </ul>

    RSSPage.php

    ...
    class RSSPage_Controller extends Page_Controller {

    function My_Feeds($url) {
    // require_once("../framework/thirdparty/simplepie/simplepie.inc");
       require_once("../framework/thirdparty/simplepie2/autoloader.php");
    $this->feed = new SimplePie();
       $this->feed->set_feed_url($url);
       $this->feed->set_cache_location("../silverstripe-cache/");

       $this->feed->init();
       $this->feed->handle_content_type();

    $items = new ArrayList();

    if($this->feed->get_items()) {
    foreach ($this->feed->get_items() as $item) {
    $items->push(new ArrayData(array(
    'Link' => $item->get_permalink(),
          'Title' => $item->get_title(),
          'Date' => $item->get_date('j F Y | g:i a'),
    'Description' => $item->get_description()
          )));

    }

    }
    return $items;
    }

    }
    ...

    QED -> have fun!

  • neilcreagh
    Avatar
    Community Member
    90 Posts

    Re: Yet another SimplePie Question (this time for SS3) Link to this post

    Thank you dinResita!
    A very helpful post

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