From f1db65d6b6142af426c2a6b3cc0274643f674824 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Sun, 1 Jul 2012 21:25:57 +1200 Subject: [PATCH] API: add api for setting a custom template on a RSS feed. 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. --- api/RSSFeed.php | 27 ++++++++++++++++++++++++++- tests/api/RSSFeedTest.php | 14 ++++++++++++++ tests/templates/RSSFeedTest.ss | 6 ++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/templates/RSSFeedTest.ss diff --git a/api/RSSFeed.php b/api/RSSFeed.php index 5f8f7ed6c..08bcbbe07 100644 --- a/api/RSSFeed.php +++ b/api/RSSFeed.php @@ -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 @@ -80,6 +81,11 @@ class RSSFeed extends ViewableData { */ protected $etag; + /** + * @var string + */ + protected $template = 'RSSFeed'; + /** * Constructor * @@ -203,10 +209,29 @@ class RSSFeed extends ViewableData { 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; + } } /** diff --git a/tests/api/RSSFeedTest.php b/tests/api/RSSFeedTest.php index af6ed10fd..fc84b8d41 100644 --- a/tests/api/RSSFeedTest.php +++ b/tests/api/RSSFeedTest.php @@ -43,6 +43,20 @@ class RSSFeedTest extends SapphireTest { $this->assertContains('ItemC AltContent', $content); } + public function testRenderWithTemplate() { + $rssFeed = new RSSFeed(new ArrayList(), "", "", ""); + $rssFeed->setTemplate('RSSFeedTest'); + + $content = $rssFeed->feedContent(); + + $this->assertContains('Test Custom Template', $content); + + $rssFeed->setTemplate('RSSFeed'); + $content = $rssFeed->feedContent(); + + $this->assertNotContains('Test Custom Template', $content); + } + public function setUp() { parent::setUp(); Director::setBaseURL('/'); diff --git a/tests/templates/RSSFeedTest.ss b/tests/templates/RSSFeedTest.ss new file mode 100644 index 000000000..fc320a39e --- /dev/null +++ b/tests/templates/RSSFeedTest.ss @@ -0,0 +1,6 @@ + + + + Test Custom Template + + \ No newline at end of file