User Tools

Site Tools


txpun:feeds:integrate

Parser Integration

Examples of SimpliePie integration into the different projects.

Textpattern

Integrate SimplePie into your Textpattern templates and posts with this plug-in.

Plugin Instructions

SimplePie Plugin for Textpattern adds one tag to your Textpattern installation:

<txp:feed>http://example.com/feed.xml</txp:feed>

Attributes

  • items attribute – Limits the number of items returned. If you set this value to 5, then you'll get back the 5 most recent posts. If there's a feed with fewer than 5 posts, SimplePie will return all of them. Defaults to all.
  • showdesc attribute – Determines whether the description should be shown or not. If set to false, descriptions are omitted, and the ordered list will display only the linked item titles with no special formatting. Defaults to true.
  • showdate attribute – Displays the date of the news item. Accepts anything that's allowed in PHP's date() function. Defaults to blank.
  • shortdesc attribute – Strips all tags from the item's description and limits the number of characters that are displayed. Accepts any numeric value. If more characters are allowed than are in the description, the entire description will be displayed. If the text wasn't cut at the end of a sentence (ending with a period, exclamation point, or question mark), an ellipsis will be added to the end of the text. Defaults to all characters.
  • showtitle keyword – Determines whether the built-in feed title is displayed or not. Defaults to true.
  • alttitle keyword – Displays a custom title in place of the feed's built-in title. Defaults to blank.
  • error keyword – Displays a custom error message for when there is a problem retrieving the feed. Defaults to the standard error messages.

Plugin Usage

This is how the plugin is being used on Textpattern Planet:

Sample tag

<txp:feed items="5" showdate="l &#183; F j, Y &#183; h:i:s A" shortdesc="210" alttitle="Developer Weblog" >
http://textpattern.com/rss/?section=weblog
</txp:feed>

Plugin Code

Version 1.2 of the plugin code, slightly modified

global $txpcfg;
if (!class_exists('SimplePie')) require($txpcfg['txpath'].'/lib/simplepie.inc');
 
function feed($atts, $thing='') {
	global $txpcfg;
	$BLOGROOT = explode('/index.php', $_SERVER["PHP_SELF"]);
	$BLOGROOT = $BLOGROOT[0];
	$TXPROOT = $BLOGROOT . '/textpattern';
 
	$input = $thing;
	$argv = $atts;
 
	$feed = new SimplePie();
	$feed->feed_url($input);
        $feed->cache_max_minutes(30);
	$feed->cache_location($txpcfg['txpath'] . '/tmp');
	$feed->bypass_image_hotlink();
	$feed->bypass_image_hotlink_page($TXPROOT . '/lib/simplepie_image_handler.php');
	$success = $feed->init();
 
	if ($success && $feed->data) {
		$flink = $feed->get_feed_link();
		$ftitle = $feed->get_feed_title();
 
		$output='';
		$output .= '<div class="simplepie">';
		if (!isset($argv['showtitle']) || empty($argv['showtitle']) || $argv['showtitle'] == "true") {
			if (isset($argv['alttitle']) && !empty($argv['alttitle'])) {
				if ($ftitle != '' && $flink != '') $output .= "<h3><a href=\"$flink\">" . $argv['alttitle'] . "</a></h3>";
				else if ($ftitle != '') $output .= "<h3>" . $argv['alttitle'] . "</h3>";
			}
			else {
				if ($ftitle != '' && $flink != '') $output .= "<h3><a href=\"$flink\">$ftitle</a></h3>";
				else if ($ftitle != '') $output .= "<h3>$ftitle</h3>";
			}
		}
		$output .= '<ul>';
 
		$max = $feed->get_item_quantity();
		if (isset($argv['items']) && !empty($argv['items'])) $max = $feed->get_item_quantity($argv['items']);
 
		for($x=0; $x<$max; $x++) {
			$item = $feed->get_item($x);
			$link = $item->get_permalink();
			$title = StupefyEntities($item->get_title());
			$full_desc = StupefyEntities($item->get_description());
			$desc = $full_desc;
 
			if (isset($argv['shortdesc']) && !empty($argv['shortdesc'])) {
				$suffix = '...';
				$short_desc = trim(str_replace("\n", ' ', str_replace("\r", ' ', strip_tags(StupefyEntities($item->get_description())))));
				$desc = substr($short_desc, 0, $argv['shortdesc']);
				$lastchar = substr($desc, -1, 1);
				if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?') $suffix='';
				$desc .= $suffix;
			}
 
			if (isset($argv['showdesc']) && !empty($argv['showdesc']) && $argv['showdesc']==='false') {
				if (isset($argv['showdate']) && !empty($argv['showdate'])) {
					$output .= "<li><strong><a href=\"$link\">$title</a> </strong> <br /> <em> <span class=\"date\">" . $item->get_date($argv['showdate']) . "</span> </em> </li>";
				} else {
					$output .= "<li><a href=\"$link\">$title</a></li>";
				}
			} else {
				if (isset($argv['showdate']) && !empty($argv['showdate'])) {
					$output .= "<li><strong><a href=\"$link\">$title</a> </strong> <br /> <em> <span class=\"date\">" . $item->get_date($argv['showdate']) . "</span> </em> <br />$desc</li>";
				} else {
					$output .= "<li><strong><a href=\"$link\">$title</a></strong><br />$desc</li>";
				}
			}
		}
 
		$output .= '</ul>';
		$output .= '</div>';
	}
	else {
		if (isset($argv['error']) && !empty($argv['error'])) $output = $argv['error'];
		else if (isset($feed->error)) $output = $feed->error;
	}
 
	return $output;
}
 
// SmartyPants 1.5.1 changes rolled in May 2004 by Alex Rosenberg, http://monauraljerk.org/smartypants-php/
function StupefyEntities($s = '') {
	$inputs = array('&#8211;', '&#8212;', '&#8216;', '&#8217;', '&#8220;', '&#8221;', '&#8230;', '&#91;', '&#93;');
	$outputs = array('-', '--', "'", "'", '"', '"', '...', '[', ']');
	$s = str_replace($inputs, $outputs, $s);
	return $s;
}

DokuWiki

DokuWiki includes the SimplePie parser as of release 2006-11-06.

Syntax Example

{{rss>http://forum.dokuwiki.org/forum.php?req=rss}}

Output

txpun/feeds/integrate.txt · Last modified: 2008/12/06 13:55 (external edit)