Quick Umbraco tip: Display twitter feeds on your site

First, install the Ferguson Moriyama Feed Cache package for Umbraco. Make sure you read the install instructions which explain how to get the package to run as a scheduled task.

Grab the URL for your twitter feed, mine is http://search.twitter.com/search.atom?lang=en&q=from%3Adarrenferguson. You can turn any twitter search into an atom feed by visiting search.twitter.com and clicking on the ‘Feed for this query’ button after your search.

Next, configure the Feed Cache package to cache your feed. Open <webroot>/umbraco/plugins/FergusonMoriyama/FeedCache/feeds.config and add an entry like the following:

<?xml version="1.0"?>
<feeds>
	<feed>
		<url>http://search.twitter.com/search.atom?q=from%3Adarrenferguson</url>
		<localFile>twit.xml</localFile>
	</feed>
</feeds>

Wait a little while until twit.xml appears in the <webroot>/umbraco/plugins/FergusonMoriyama/FeedCache folder. If it doesn’t there is probably something wrong with your scheduled task setup.

Use the following XSLT macro to display tweets on your site:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [<!ENTITY nbsp "&#x00A0;">]>
<xsl:stylesheet
 version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxml="urn:schemas-microsoft-com:xslt"
 xmlns:umbraco.library="urn:umbraco.library"
 exclude-result-prefixes="msxml umbraco.library Atom"
 xmlns:Atom="http://www.w3.org/2005/Atom">
    
    <xsl:variable name="y" select="document('../umbraco/plugins/FergusonMoriyama/FeedCache/twit.xml')/Atom:feed"/>
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    
    <xsl:template match="/">
        <xsl:for-each select="$y/Atom:entry">
            <p>
                <em>
                    <xsl:value-of select="umbraco.library:FormatDateTime(Atom:published, 'MMMM d, yyyy @ H:mm')"/>
                </em>
		<xsl:value-of select="Atom:content" disable-output-escaping="yes"/>
            </p>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Obviously you can just change your twitter search to look for hash tags, Umbraco related tweets or whatever you want. There is also a lot more information in the atom feed than I am displaying above and the XLST can be modified to your liking.

Comments

Leave a comment