The Blogs Page: The feed digest view helper
Saturday, April 17, 2010
The feed digest helper is used to fetch a list of the most recent items posted on a blog, using its RSS feed.
The feed digest class is named Default_View_Helper_FeedDigest and stored in the /application/views/helpers directory in a file called FeedDigest.php.
The basic skeleton of the class is as follows:
class Default_View_Helper_FeedDigest extends Zend_View_Helper_Abstract { public function feedDigest($uri, $maxItems = 5) { // Implementation of view helper ... } }
The Zend Framework documentation recommends that the helper either extends the class Zend_View_Helper_Abstract or implements the interface Zend_View_Helper_Interface. This is to provide support for the setView method, which will be called to give the helper automatic access to the view object.
The class returns the HTML that should be used to render the digest. The code that fetches the digest information and turns it into an unordered HTML list is wrapped in a try/catch block; this allows any feed errors to be caught.
try
{
// Fetch feed digest
...
}
catch (Exception $e)
{
// Feed import failed
$html = '';
}
return $html;The first step is to perform the feed import, using the URI supplied:
// Import feed $feed = Zend_Feed::import($uri);
The next step is to check the number of elements returned; if there are none, we need to render an appropriate meesage:
// Generate digest of feed if ($feed->count() > 0) { // Render contents of feed .. } else { $html = ''; }
If there are feed entries, these need to be converted into a list of links, with the appropriate URL and text; the number of entries returned is limited by the $maxItems parameter passed to the helper:
$items = array(); $count = 0; foreach ($feed as $item) { // Add the item to the list $pubDate = date('d M Y', strtotime($item->pubDate)); $items[] = sprintf('<a href="%s" rel="nofollow">%s - %s</a>', $item->link, $pubDate, $item->title); // Check whether the maximum item limit has been reached $count++; if ($count >= $maxItems) { break; } }
Finally the array is rendered as an unordered list:
$html = $this->view->htmlList($items, false, null, false);
Because Zend_View_Helper_Abstract supports setView, and the helper is derived from that class, it can access other helpers via $this->view.
The second parameter passed to the HtmlList helper (false) indicates an unordered list; true would indicate an ordered list.
The fourth parameter is set to false, to indicate that the text in each array element should not be escaped; this is essential as they contain HTML links that should be rendered verbatim.

Practical Web 2.0 Applications with PHP (Expert's Voice) by Quentin Zervaas
Beginning Databases with PostreSQL: From Expert to Professional 2nd Edition: From Novice to Professional by Neil Matthew, Richard Stones
[...] I have described how to use Zend_Feed to retrieve a digest of a blog’s latest entries in a previous post. In the overall implementation described in my series of posts, this digest is fetched every time a [...]