Tutorials

Silverstripe DataObjectSet and Templates

Posted by Mo on 3 February 2010 | 6 Comments

Tags: ,

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.

Step 1: Setting up your model and controller.

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
}
}

Step 2: Getting our RSS feed. 

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.

Step 3: Setting up our DataObjectSet

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.

Step 4: Setting up your template.

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.

TrackBacks

  • Denzel Current on 22/11/2011 8:17pm

    Thank you ever so for you blog article.Much thanks again. Great.

  • Melody Howells on 24/11/2011 1:18am

    Thank you for your article.Really looking forward to read more. Really Cool.

  • Ari Stapleton on 24/11/2011 4:26am

    I really enjoy the article. Keep writing.

  • Kaylin Mcfarlin on 24/11/2011 1:47pm

    Enjoyed every bit of your blog post. Want more.

  • Danna Covell on 24/11/2011 3:17pm

    Really enjoyed this article post. Really Cool.

  • Bria Leasure on 24/11/2011 8:03pm

    I appreciate you sharing this blog.Really looking forward to read more. Really Cool.

  • Madyson Steinman on 25/11/2011 1:02am

    I cannot thank you enough for the article post.Much thanks again. Cool.

  • Scarlet Hernandez on 27/11/2011 3:09am

    Wow, great post.Really thank you! Much obliged.

  • Gideon Hafner on 28/11/2011 2:52am

    Great article.Thanks Again. Awesome.

  • Daniella Biles on 29/11/2011 9:29pm

    Im grateful for the article.Much thanks again. Want more.

  • Imiapasxolisi on 01/12/2011 6:31pm

    Thanks again for the article post.Really looking forward to read more. Cool.

  • Charlie Mckeown on 03/12/2011 1:32am

    Awesome article post.Really looking forward to read more. Cool.

  • Henry Curl on 03/12/2011 2:20am

    I cannot thank you enough for the article post.Much thanks again. Will read on...

  • Kaylen Gregoire on 04/12/2011 7:57pm

    Thanks so much for the article post.Much thanks again. Great.

  • Ralph Guth on 08/12/2011 1:43am

    Thank you for your article.Really looking forward to read more. Awesome.

  • Rayna Norberg on 09/12/2011 3:24am

    I loved your article.Much thanks again. Great.

  • Melody Schmid on 09/12/2011 3:57am

    A round of applause for your blog article.Thanks Again. Keep writing.

  • Brenton Canady on 13/12/2011 4:29pm

    I value the article. Cool.

  • Matias Hensel on 14/12/2011 9:36pm

    Thanks so much for the article.Much thanks again. Want more.

  • Sonia Scalf on 15/12/2011 2:24pm

    Really informative blog.Really thank you! Will read on...

  • Yuridia Dickens on 15/12/2011 3:42pm

    Thanks-a-mundo for the blog article. Fantastic.

  • Jamir Gose on 17/12/2011 7:00pm

    I appreciate you sharing this article.Really thank you! Really Great.

  • Konnor Oleson on 17/12/2011 9:58pm

    I am so grateful for your article post.Really thank you! Will read on...

  • Autumn Sellers on 18/12/2011 5:32am

    I really enjoy the article.Really thank you!

  • Financial Planner Houston on 03/01/2012 3:53pm

    This is one awesome article post.

  • Floirda Medicaid Eligibility on 08/01/2012 12:47am

    Say, you got a nice article post.Really thank you! Great.

  • Wesley Braddock on 11/01/2012 4:25am

    Thank you for your post.Really thank you! Really Cool.

  • Carlo Kroeger on 14/01/2012 4:39am

    I think this is a real great article post.Really thank you! Really Great.

  • Jordyn Leiker on 15/01/2012 11:37am

    I really like and appreciate your blog article.Much thanks again. Fantastic.

  • Yusuf Conners on 03/02/2012 5:58pm

    Thanks for sharing, this is a fantastic post.Much thanks again. Awesome.

  • Royce Kinsler on 03/02/2012 6:07pm

    Thanks so much for the blog article.Thanks Again. Great.

  • Dangelo Largent on 03/02/2012 6:12pm

    Thanks again for the article.Much thanks again. Will read on...

  • Kali Redford on 05/02/2012 10:54pm

    Awesome post.Really looking forward to read more. Keep writing.

  • Brendan Livermore on 05/02/2012 10:57pm

    Great article post. Much obliged.

Trackback URL for this page.

Post your comment

Comments

  • 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