Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Yet another SimplePie Question (this time for SS3)


Reply

3 Posts   594 Views

Avatar
dinResita

3 July 2013 at 9:46pm Community Member, 3 Posts

I did try to use Willr answer to topic [url=http://www.silverstripe.org/general-questions/show/15514]Simple Pie with Silverstripe[/url], 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?

Avatar
dinResita

11 July 2013 at 10:39am (Last edited: 11 July 2013 10:39am), Community Member, 3 Posts

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.
[url=http://doc.silverstripe.org/framework/en/topics/debugging]The debugging documentation[/url] 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!

Avatar
neilcreagh

27 February 2014 at 3:56am Community Member, 94 Posts

Thank you dinResita!
A very helpful post