Skip to content
This repository
Browse code

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.
  • Loading branch information...
commit f1db65d6b6142af426c2a6b3cc0274643f674824 1 parent 23ed533
Will Rossiter authored July 01, 2012
27  api/RSSFeed.php
@@ -8,6 +8,7 @@
8 8
  * @subpackage integration
9 9
  */
10 10
 class RSSFeed extends ViewableData {
  11
+
11 12
 	/**
12 13
 	 * Casting information for this object's methods.
13 14
 	 * Let's us use $Title.XML in templates
@@ -81,6 +82,11 @@ class RSSFeed extends ViewableData {
81 82
 	protected $etag;
82 83
 
83 84
 	/**
  85
+	 * @var string
  86
+	 */
  87
+	protected $template = 'RSSFeed';
  88
+
  89
+	/**
84 90
 	 * Constructor
85 91
 	 *
86 92
 	 * @param SS_List $entries RSS feed entries
@@ -203,10 +209,29 @@ function outputToBrowser() {
203 209
 	function feedContent() {
204 210
 		$prevState = SSViewer::get_source_file_comments();
205 211
 		SSViewer::set_source_file_comments(false);
206  
-		$content = str_replace(' ', ' ', $this->renderWith('RSSFeed'));
  212
+		$content = str_replace(' ', ' ', $this->renderWith($this->getTemplate()));
207 213
 		SSViewer::set_source_file_comments($prevState);
208 214
 		return $content;
209 215
 	}
  216
+
  217
+	/**
  218
+	 * Set the name of the template to use. Actual template will be resolved
  219
+	 * via the standard template inclusion process.
  220
+	 *
  221
+	 * @param string
  222
+	 */
  223
+	public function setTemplate($template) {
  224
+		$this->template = $template;
  225
+	}
  226
+
  227
+	/**
  228
+	 * Returns the name of the template to use.
  229
+	 *
  230
+	 * @return string
  231
+	 */
  232
+	public function getTemplate() {
  233
+		return $this->template;
  234
+	}
210 235
 }
211 236
 
212 237
 /**
14  tests/api/RSSFeedTest.php
@@ -43,6 +43,20 @@ function testRSSFeed() {
43 43
 		$this->assertContains('<description>ItemC AltContent</description>', $content);
44 44
 	}
45 45
 
  46
+	public function testRenderWithTemplate() {
  47
+		$rssFeed = new RSSFeed(new ArrayList(), "", "", "");
  48
+		$rssFeed->setTemplate('RSSFeedTest');
  49
+
  50
+		$content = $rssFeed->feedContent();
  51
+
  52
+		$this->assertContains('<title>Test Custom Template</title>', $content);
  53
+
  54
+		$rssFeed->setTemplate('RSSFeed');
  55
+		$content = $rssFeed->feedContent();
  56
+		
  57
+		$this->assertNotContains('<title>Test Custom Template</title>', $content);
  58
+	}
  59
+
46 60
 	public function setUp() {
47 61
 		parent::setUp();
48 62
 		Director::setBaseURL('/');
6  tests/templates/RSSFeedTest.ss
... ...
@@ -0,0 +1,6 @@
  1
+<?xml version="1.0"?>
  2
+<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
  3
+	<channel>
  4
+		<title>Test Custom Template</title>
  5
+	</channel>
  6
+</rss>

0 notes on commit f1db65d

Please sign in to comment.
Something went wrong with that request. Please try again.