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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Yet another SimplePie Question (this time for SS3)


Go to End


3 Posts   1675 Views

Avatar
dinResita

Community Member, 3 Posts

3 July 2013 at 9:46pm

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?

Avatar
dinResita

Community Member, 3 Posts

11 July 2013 at 10:39am

Edited: 11/07/2013 10:39am

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!

Avatar
neilcreagh

Community Member, 136 Posts

27 February 2014 at 3:56am

Thank you dinResita!
A very helpful post