Skip to content

Commit

Permalink
API: add api for setting a custom template on a RSS feed.
Browse files Browse the repository at this point in the history
See initial idea at http://open.silverstripe.org/ticket/6441. Added $template property and corresponding getters / setters for customizing the template used. Added relevant unit test.
  • Loading branch information
wilr committed Jul 1, 2012
1 parent 23ed533 commit f1db65d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
27 changes: 26 additions & 1 deletion api/RSSFeed.php
Expand Up @@ -8,6 +8,7 @@
* @subpackage integration
*/
class RSSFeed extends ViewableData {

/**
* Casting information for this object's methods.
* Let's us use $Title.XML in templates
Expand Down Expand Up @@ -80,6 +81,11 @@ class RSSFeed extends ViewableData {
*/
protected $etag;

/**
* @var string
*/
protected $template = 'RSSFeed';

/**
* Constructor
*
Expand Down Expand Up @@ -203,10 +209,29 @@ function outputToBrowser() {
function feedContent() {
$prevState = SSViewer::get_source_file_comments();
SSViewer::set_source_file_comments(false);
$content = str_replace(' ', ' ', $this->renderWith('RSSFeed'));
$content = str_replace(' ', ' ', $this->renderWith($this->getTemplate()));
SSViewer::set_source_file_comments($prevState);
return $content;
}

/**
* Set the name of the template to use. Actual template will be resolved
* via the standard template inclusion process.
*
* @param string
*/
public function setTemplate($template) {
$this->template = $template;
}

/**
* Returns the name of the template to use.
*
* @return string
*/
public function getTemplate() {
return $this->template;
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/api/RSSFeedTest.php
Expand Up @@ -43,6 +43,20 @@ function testRSSFeed() {
$this->assertContains('<description>ItemC AltContent</description>', $content);
}

public function testRenderWithTemplate() {
$rssFeed = new RSSFeed(new ArrayList(), "", "", "");
$rssFeed->setTemplate('RSSFeedTest');

$content = $rssFeed->feedContent();

$this->assertContains('<title>Test Custom Template</title>', $content);

$rssFeed->setTemplate('RSSFeed');
$content = $rssFeed->feedContent();

$this->assertNotContains('<title>Test Custom Template</title>', $content);
}

public function setUp() {
parent::setUp();
Director::setBaseURL('/');
Expand Down
6 changes: 6 additions & 0 deletions tests/templates/RSSFeedTest.ss
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Test Custom Template</title>
</channel>
</rss>

0 comments on commit f1db65d

Please sign in to comment.