Back

Load Latest Tweet (Twitter) into Page

Description

This code will pull in the latest tweet from any Twitter account and display it in your page. It also does the work of converting addresses, hash tags and mentions into clickable links.

Code

#set($startWithUser = false)
#set($twitterUsername = "okchristian")

#set($path = "http://twitter.com/statuses/user_timeline/$!{twitterUsername}.rss")
#set($_dummy = $xmltool.read($path))
#set($twitterData = $xmltool.parse($_dummy))
#set($twitterDesc = $twitterData.find('channel/item/description'))


#set($latestTweet = $twitterDesc.get(0).toString())

## Strip off beginning username if it's not wanted
#if($startWithUser != true)
	#set($latestTweet = $latestTweet.replaceAll("(?i)$!{twitterUsername}: ",""))
#end

## Strip off <description> tags
#set($latestTweet = $latestTweet.replaceAll("<\/?description>",""))

## Replace URL text with an actual link
#set($latestTweet = $latestTweet.replaceAll("[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+","<a href='$0' target='_blank'>$0</a>"))

## Replace "#whatever" with a link to "http://twitter.com/search?q=%23whatever"
#set($latestTweet = $latestTweet.replaceAll("(?<=#)((\w+))","<a href='http://twitter.com/search\?q=%23$0' target='_blank'>$0</a>"))

## Replace "@whoever" with a link to "http://www.twitter.com/whoever"
#set($latestTweet = $latestTweet.replaceAll("(?<=@)((\w+))","<a href='http://twitter.com/$0' target='_blank'>$0</a>"))
 
	<h2>@<a href='http://twitter.com/$!{twitterUsername}' target='_blank'>$!{twitterUsername}</a> Twitter</h2> 
	<p>$!{latestTweet}</p>