====== HowTo create RSS feed using Limb2 ====== RSS is an essential part of every site with rich content. We'd like to show how easy it is to create a RSS feed with Limb and WACT. Say, we'd like to have the RSS feed of the latest news objects and documents. Let's create a simple **last-news-feed.php** script, it's going to accomplish the following tasks: * fetching of news and documents objects * merge fetched objects and sort them by last modified date, if date is equal(very rare), then additionally sort by node id(since they are incremental) * normalize hrefs in order to make them absolute, href='/root/test/../new' should become href='http://yourdomain.com/root/new' * pass the processed dataset into WACT template We think the following code should be self explanatory: $b['node_id']) return -1; else return 1; } return ($date1 > $date2) ? -1 : 1; } //function that normalizes hrefs in $content using $base_path to resolve relative links function fix_hrefs($base_path, $content) { $GLOBALS['base_path'] = $base_path; return preg_replace_callback('~href=("|\')?([^"\'>\s]+)("|\')?~', '_replace_callback', $content); } //preg_replace_callback function, does the dirty job of regular expression replacing function _replace_callback($matches) { if(preg_match('~(https?|ftp)://~', $matches[2])) return $matches[0]; $url = get_qualified_url($GLOBALS['base_path'], $matches[2]); return "href='{$url}'"; } //small helper function that actually normalizes the href function get_qualified_url($base_path, $raw_path) { $path = $raw_path; if($path{0} != '/') $path = uri :: resolve_path($base_path . '/' . $path); return 'http://' . sys :: hostname() . $path; } $counter = 0; $params = array( 'limit' => 3, 'order' => array('modified_date' => 'DESC') ); $arr_news = fetch('news_object', $counter, $params, 'fetch_accessible'); $params = array( 'limit' => 3, 'order' => array('modified_date' => 'DESC') ); $arr_documents = fetch('document', $counter, $params, 'fetch_accessible'); $arr = array_merge($arr_documents, $arr_news); uasort($arr, 'sorter'); foreach($arr as $key => $data) { $arr[$key]['annotation'] = fix_hrefs($data['path'], $data['annotation']); $arr[$key]['path'] = get_qualified_url($data['path'], $data['path']); } $ds = new array_dataset($arr); $template = new template('/rss/last_news.rss'); $template->set('xml', ''); $news =& $template->find_child('last_news'); $news->register_dataset($ds); header("Content-Type: application/xml"); $template->display(); ?> And here goes the template itself: {$xml} ]> YourDomain.com last news http://YourDomain.com YourDomain.com site en {$title} {$path} Note that we use **** tag in order to sanitize the html content and we use **** tag to display properly formatted date. Now place the script to **PROJECT_DIR/last-news-feed.php** and template to **PROJECT_DIR/design/main/templates/rss/last_news.rss**. Make a neat link to your RSS feed somewhere in the layout, sort like ]] and...that really should be it!