NewsFeaturesDownloadsDevelopmentSupportAbout Us

How to use the RSS aggregator

From LifeType Wiki

How to use the RSS aggregator

Every template in LifeType has an additional object available that works as an RSS aggregator, should you wish to integrate data from external RSS feeds in your blog. There is no way to configure it from the adm interface of LifeType, and the only way to use it is to write some additional Smarty code.

A first good place to start is the Smarty documentation: http://smarty.php.net/manual/en/. Regarding the RSS parser, it is based on the open-source Magpie RSS parser, which can be found here: http://magpierss.sourceforge.net/. The Magpie RSS parser supports RSS 0.9 up to RSS 1.0.

The easiest way to add some syndicated RSS content to our page is with the following snippet of code:

{if $rss->parse("http://slashdot.org/slashdot.rdf")}
 {foreach from=$rss->getItems() item=rssItem}
   <a href="{$rssItem->getLink()}">{$rssItem->getTitle()}</a>
{/foreach} {else} There was an error parsign the feed! {/if}

The *$rss* object, of type RssParser is the one that deals with fetching the data from the source and parsing it, and its *parse()* method takes care of parsing it. As a parameter, it takes a valid RSS/RDF feed and it will return *true* if the parsing was successful, or false otherwise. If the parsing was successful, we will be able to use the *getItems()* method from the *$rss* object that will return an array of [RssItem] objects containing all the items that were found in the feed.

Once we have the array of items, we can use a foreach construction to loop through all of them. Smarty will assign the current object to the variable given as the "item" parameter. Once we have one of the items, the two most common methods that we can use are the RssItem::getLink() and RssItem::getTitle, that will return the link to the item and the title given to the link in the RSS feed (see the page describing the RssItem object for more information on its methods)

This bit of code can be added to any existing template. For example, we might want to add an additional section on the right panel of the 'blueish' template containing the latest headlines of our favourite sites, or we can add a new template file to our current template set. However, we should keep in mind that fetching a remote web page and parsing it, is not a task that is carried out in a couple of seconds and it might take some time, specially if we're fetching the headlines from a busy site.

As an example, we can add an additional page to our blueish template called rss.template that will show the headlines of a couple of news web sites (Yahoo! News and BBC News). These are the contents of the file:

  {include file="blueish/header.template"}
   {if $rss->parse("http://rss.news.yahoo.com/rss/topstories")}
    {assign var=channel value=$rss->getChannel()}
    <h1>{$channel->getTitle()}</h1>
    <p>{$channel->getDescription()}</p>
    <ul style="list-style: none;">
     {foreach from=$rss->getItems() item=rssItem}
     <li>
      <a href="{$rssItem->getLink()}">{$rssItem->getTitle()}</a><br/>
      <p>
       {$rssItem->getDescription()}
      </p>
     </li>
    {/foreach}
    </ul>
  {/if}
     
   {if $rss->parse("http://news.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss091.xml")}
   {assign var=channel value=$rss->getChannel()}
   <h1>{$channel->getTitle()}</h1>
   <p>{$channel->getDescription()}</p>
   <ul style="list-style: none;">
    {foreach from=$rss->getItems() item=rssItem}
    <li>
     <a href="{$rssItem->getLink()}">{$rssItem->getTitle()}</a><br/>
     <p>
      {$rssItem->getDescription()}
     </p>
    </li>
   {/foreach}
   </ul>
   {/if}
   {include file="blueish/footer.template"}

This example is longer but not more complex, since it basically uses all concepts that were previously introduced, and adds some formatting. It also adds the new RssChannel object, that holds some information about the channel that we just parsed such as the title, link and description.

Now in order to add a static link to our new customized page with RSS feeds we can for example edit the file templates/standard/footer.template* and add the following changes wherever we would like the link to the page to appear:

   <a href="{$url->templatePage("rss")}">My RSS Feeds</a></li>

We are using the templatePage() method which takes as a parameter the name of the template file we'd like to render (without the trailing .template) to make LifeType automatically generate the right URL for us.

Support for Enclosures in RSS 2.0

The previous example could be expanded if the feed we are dealing with supports enclosures. Only feeds formatted in RSS 2.0 offer support for enclosures.

{if $rss->parse("http://feeds.feedburner.com/Beyondjazz")}
 {foreach from=$rss->getItems() item=rssItem}
   <a href="{$rssItem->getLink()}">{$rssItem->getTitle()}</a>
   {if $rssItem->hasEnclosures()}
     <p>This feed has <b>{$rssItem->getNumEnclosures()}</b> enclosures:<br/>
     {foreach from=$rssItem->getEnclosures() item=enclosure}
          <a href="{$enclosure->getUrl()}">{$enclosure->getUrl()}</a>, 
       (Type: {$enclosure->getMimeType()}) {$enclosure->getSize()} bytes.<br/>
     {/foreach}
     </p>
   {/if}
 {/foreach}
{else}
  There was an error parsign the feed!
{/if}

The RssItem class provides the hasEnclosures(), getNumEnclosures() and getEnclosures() methods that return information about the item and its enclosures. In order to obtain the enclosures from an item, the RssItem::getEnclosures() method should be used. It will return an array of RssEnclosure objects whose methods are getMimeType(), getLength() and getUrl(). The purpose of these methods is quite clear as depicted above.