Display latest tweets in SilverStripe

Displaying your latest tweet using Javascript is fairly straight forward and this method can be used to display a tweet in a SilverStripe template. The Javascript might look a bit like this:

$.ajaxSetup({ cache: true });
$.getJSON("http://twitter.com/statuses/user_timeline/SwipeStripe.json?callback=?", function(data) {
    $(".tweet").html(data[0].text);
});

Another method is to not use Javacript at all, instead grabbing the latest tweet or tweets using PHP in your Page_Controller:

public function Tweet() {
    
  $status = null;
  $userID = 12345; //Your twitter ID here
  $url = "https://api.twitter.com/1/statuses/user_timeline/{$userID}.xml?count=1&include_rts=1";

  if ($xml = simplexml_load_file($url)) {

    foreach($xml->status as $status){
      $text = $status->text;

      $dom = new DOMDocument;
      $dom->loadXML($text->asXML());

      $status = $dom->documentElement->textContent;
    }
  }
  return $status;
}


  public function TweetCache() {
    //Cache for an hour
    return date('d-M-y:H');
  }

In the template you can then cache the tweet for as long as you like:

<% cached TweetCache %>
<p>
  $Tweet
</p>
<% end_cached %>

I think this is a reasonably good method and saves asking twitter for the latest tweet each page load.