For some reason, I always forget how to generate a custom DataObjectSet in a Silverstripe method, that I can then return to my template to correctly render in a control loop. Because of this, I am going to write an example tutorial, using my Twitter RSS feed and SimpleXML (which should be part of the PHP5 core for most installs) as an example.
In this tutorial, I am going to add my Twitter feed to my Page class. That way it can appear on all pages, and be available to child classes as well.
To do this I will first add a TwitterURL field to my model, as well as the relevant fields to the CMS:
class Page extends SiteTree {
static $db = array(
'TwitterURL' => 'Text'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Misc',new TextField('TwitterURL', 'Twitter URL'));
return $fields;
}
}
Next, I will add the method I will call in my template to my controller:
class Page_Controller extends ContentController {
public function init() {
parent::init();
}
public function getTwitterFeed($limit = 5) {
// More to go here
}
}
Now the basic setup is complete, now we need to write some code to extract our Twitter feed and then loop through each posted item in the feed channel. Here is how we would do this using SimpleXML (there are other methods of doing something similar, including PHP Dom and SimplePie):
class Page_Controller extends ContentController {
...
public function getTwitterFeed($limit = 5) {
$Twitter = simplexml_load_file($this->TwitterURL);
$i = 0;
foreach($Twitter->channel->item as $item) {
if($i < $limit) {
// Some data object stuff will go here shortly
}
}
}
}
Admitidly this wont do much at the moment, just loop through the items in your RSS feed until you the end, or the loop reaches the limit set by $limit.
Now we have our loop, the interesting bit can begin. Now we need to create a new DataObjectSet and for each item, push an array of data to it. This will be the data that is then sent to the template for rendering.
Now you need to alter your getTwitterFeed() method as below:
class Page_Controller extends ContentController {
...
public function getTwitterFeed($limit = 5) {
$Twitter = simplexml_load_file($this->TwitterURL);
$result = new DataObjectSet();
$i = 0;
foreach($Twitter->channel->item as $item) {
if($i < $limit) {
// To retrieve the publish date in a format that Silverstripe
// will be able to play with, we need to convert it to a date object.
$Date = new Date('Date');
$Date->setValue((string)$item->pubDate);
// Put info from RSS into an Array, then push into $result
$result->push(new ArrayData(array(
'Title' => (string)$item->title,
'Date' => $Date
)));
$i++;
}
}
return $result;
}
}
Now what is happening is that we create a new DataObjectSet called $result. For each iteration of the RSS feed, we take the title and date from the feed, convert and cast then as necessary then put that into an array. Silverstripe requires that this data be formatted using an ArrayData object, so we use that first, before dding the data to our DataObjectSet.
Finally we return the completed DataObjectSet, from this example, the variables $Title and $Date will be available in your control loop.
Now we have all the data in in right format, the final step is to render it in your template. In order to render each tweet as a paragraph, you would do:
<% control getTwitterFeed %>
<p>
$Title<br/>
Posted: $Date.Ago
</p>
<% end_control %>
By default, this method will display the 5 most recent tweets. If you wanted to show a different number, for example 10, you could use:
<% control getTwitterFeed(10) %>
<p>
$Title<br/>
Posted: $Date.Ago
</p>
<% end_control %>
Thats it, if you want to include additional info, like Link and Description, you simply have to add those items to your array in your method. These will then be available to you in your template.
Thank you ever so for you blog article.Much thanks again. Great.
Thank you for your article.Really looking forward to read more. Really Cool.
I really enjoy the article. Keep writing.
Enjoyed every bit of your blog post. Want more.
Really enjoyed this article post. Really Cool.
I appreciate you sharing this blog.Really looking forward to read more. Really Cool.
I cannot thank you enough for the article post.Much thanks again. Cool.
Wow, great post.Really thank you! Much obliged.
Great article.Thanks Again. Awesome.
Im grateful for the article.Much thanks again. Want more.
Thanks again for the article post.Really looking forward to read more. Cool.
Awesome article post.Really looking forward to read more. Cool.
I cannot thank you enough for the article post.Much thanks again. Will read on...
Thanks so much for the article post.Much thanks again. Great.
Thank you for your article.Really looking forward to read more. Awesome.
I loved your article.Much thanks again. Great.
A round of applause for your blog article.Thanks Again. Keep writing.
I value the article. Cool.
Thanks so much for the article.Much thanks again. Want more.
Really informative blog.Really thank you! Will read on...
Thanks-a-mundo for the blog article. Fantastic.
I appreciate you sharing this article.Really thank you! Really Great.
I am so grateful for your article post.Really thank you! Will read on...
I really enjoy the article.Really thank you!
This is one awesome article post.
Say, you got a nice article post.Really thank you! Great.
Thank you for your post.Really thank you! Really Cool.
I think this is a real great article post.Really thank you! Really Great.
I really like and appreciate your blog article.Much thanks again. Fantastic.
Thanks for sharing, this is a fantastic post.Much thanks again. Awesome.
Thanks so much for the blog article.Thanks Again. Great.
Thanks again for the article.Much thanks again. Will read on...
Awesome post.Really looking forward to read more. Keep writing.
Great article post. Much obliged.
That way it can appear on all pages, and be available to child classes as well.
Posted by local personals free, 04/02/2012 11:33am (2 days ago)
All these tiny details are made with lot of background knowledge. I like it a lot.
Posted by Marc Jacobs outlet, 04/02/2012 6:51am (2 days ago)
good@@
Posted by canada goose jackets, 03/02/2012 3:01am (3 days ago)
Tory Burch Outlet
Posted by Tory Burch Outlet, 03/02/2012 2:48am (3 days ago)
push an array of data to it. This will be the data that is then sent to the template for rendering.
Posted by templates, 13/07/2011 3:22pm (7 months ago)
thanks
Posted by tadd, 04/06/2010 9:20am (2 years ago)
RSS feed for comments on this page | RSS feed for all comments