This document contains information for an outdated version (3.0) and may not be maintained any more.

If some of your projects still use this version, consider upgrading as soon as possible.

RSS Feed

Introduction

Generating RSS/Atom-feeds is a matter of rendering a SS_List through the RSSFeed class.

The RSSFeed class doesn't limit you to generating article based feeds, it is just as easy to create a feed of your current staff members, comments or any other custom DataObject subclasses you have defined. The only logical limitation here is that every item in the RSS-feed should be accessible through a URL on your website, so its advisable to just create feeds from sub classes of SiteTree.

If you wish to generate an RSS feed for DataObject instances, ensure they define an AbsoluteLink() method.

Usage

RSSFeed::linkToFeed($link, $title)

This line should go in your Controller subclass in the action you want to include the HTML link.

$feed = new RSSFeed(
    $list, 
    $link, 
    $title, 
    $description, 
    $titleField, 
    $descriptionField, 
    $authorField
);

Creates a new RSSFeed instance to be returned. The arguments notify SilverStripe what values to include in the feed.

Examples

Showing latest blog posts

class Page_Controller extends ContentController {
    public function init() {
        // linkToFeed will add an appropriate HTML link tag to the website 
        // <head> tag to notify web browsers that an RSS feed is available 
        // for this page. You can include as many feeds on the page as you 
        // wish as long as each as a different link. For example: 
        // ('blog/rss', 'staff/rss').
        // 
        // In this example $this->Link("rss") refers to the *rss* function
        // we define below.
        RSSFeed::linkToFeed($this->Link("rss"), "RSS feed of this blog");
        parent::init();
    }
    public function rss() {
        // Creates a new RSS Feed list
        $rss = new RSSFeed(
            $list = $this->getBlogPosts(), // an SS_List containing your feed items
            $link = $this->Link("rss"), // a HTTP link to this feed
            $title = "My feed", // title for this feed, displayed in RSS readers
            $description = "This is an example feed." // description
        );
        // Outputs the RSS feed to the user.
        return $rss->outputToBrowser();
    }
    public function getBlogPosts() {
        return BlogPage::get()->limit(10);
    }
}

Showing the 10 most recently updated pages

You can use RSSFeed to easily create a feed showing your latest Page updates. Update mysite/code/Page.php to something like this:

<?php
class Page extends SiteTree {}  
class Page_Controller extends ContentController {
    
    public function init() {
        RSSFeed::linkToFeed($this->Link() . "rss", "10 Most Recently Updated Pages");    
        parent::init();
    }
    
    public function rss() {
        $rss = new RSSFeed($this->LatestUpdates(), $this->Link(), "10 Most Recently Updated Pages", "Shows a list of the 10 most recently updated pages.");
        return $rss->outputToBrowser();
    }
    
    public function LatestUpdates() {
        return Page::get()->sort("LastEdited", "DESC")->limit(10);
    } 
}

Rendering DataObjects in a RSSFeed

DataObjects can be rendered in the feed as well, however, since they aren't explicitly SiteTree subclasses we need to include a function AbsoluteLink to allow the RSS feed to link through to the item.

If the items are all displayed on a single page you may simply hard code the link to point to a particular page.

Take an example, we want to create an RSS feed of all the Students, a DataObject we defined in the fifth tutorial.

<?php 
class Student extends DataObject {
    public function AbsoluteLink() {
        // see tutorial 5, students are assigned a project, so the 'link'
        // to view the student is based on their projects link.
        return $this->Project()->AbsoluteLink();
    }
}

Then update the Page_Controller class in mysite/code/Page.php to include an RSSFeed for all the students as we've seen before.

class Page_Controller extends ContentController {
    public function init() {
        RSSFeed::linkToFeed($this->Link("students"), "Students feed");
        parent::init();
    }       
    public function students() {
        $rss = new RSSFeed(
            $list = $this->getStudents(),
            $link = $this->Link("students"), 
            $title = "Students feed" 
        );
        return $rss->outputToBrowser();
    }
    public function getStudents() {
        return Student::get()->sort("Created", "DESC")->limit(10);
    }
}

Customizing the RSS Feed template

The default template used is framework/templates/RSSFeed.ss and includes displaying titles and links to the content. If you have a particular need for customizing the XML produced (say for additional meta data) use setTemplate.

Taking that last example, we would rewrite the students function to include a unique template (write your own XML in themes/yourtheme/templates/Students.ss)

public function students() {
    $rss = new RSSFeed(
        $list = $this->getStudents(),
        $link = $this->Link("students"), 
        $title = "Students feed" 
    );
    $rss->setTemplate('Students');
    return $rss->outputToBrowser();
}

External Sources

RSSFeed only creates feeds from your own data. We've included the SimplePie RSS-parser for accessing feeds from external sources.

API Documentation

Comments

Comment policy: Please use comments for tips and corrections about the described functionality.
Comments are moderated, we reserve the right to remove comments that are inappropriate or are no longer relevant. Use the Silverstripe Forum to ask questions.

blog comments powered by Disqus