A XML or RSS-feed reader with plain Smarty
Previous article Next articleCreate your own XML or RSS-feed reader with plain Smarty, there are no external modules or plug-ins needed!
How to use
Page
Create a new content page and set WYSIWYG off.
Copy the following code in the content of the page. Note, this template is just an example: with the parameters below you can change it to your liking.
In the first line you can add the URL of the feed you want to display at your website.
{if !empty($xml)}
<h3>{$xml->channel->title}</h3>
<h4>{$xml->channel->description}</h4>
<p>Generator: {$xml->channel->generator}<br />
URL: {$xml->channel->link}<br />
Copyright: {$xml->channel->copyright}</p>
{$cnt = 0}
{foreach $xml->channel->item as $item}
<h4><a href="{$item->link}">{$item->title}</a></h4>
<p>{$item->pubDate|date_format:'%e %B %Y'}<br />
{$item->author}: {$item->description} <a href="{$item->guid}">Read more</a></p>
{$cnt = $cnt + 1}
{if $cnt == 2}{break}{/if}
{/foreach}
{else}
<p>No feed found...</p>
{/if}
The number "2" in the $cnt line above, is the number of feed items that will be shown at your page.
Available parameters
Channel section
{$xml->channel->description}
{$xml->channel->generator}
{$xml->channel->link}
{$xml->channel->webMaster}
{$xml->channel->managingEditor}
{$xml->channel->copyright}
{$xml->channel->image->link}
{$xml->channel->image->url}
{$xml->channel->image->width}
{$xml->channel->image->height}
{$xml->channel->image->title}
Item section
{$item->title}
{$item->pubDate}
{$item->author}
{$item->description}
{$item->guid}
{$item->enclosure['url']}
{$item->enclosure['length']}
{$item->enclosure['type']}
Options
Channel image example
<a href="{$xml->channel->image->link}">
<img src="{$xml->channel->image->url}" width="{$xml->channel->image->width}" height="{$xml->channel->image->height}" alt="{$xml->channel->image->title}" />
</a>
{/if}
Item image enclosure example
<img src="{$item->enclosure['url']}" alt="{$item->title}" />
{/if}
Permissive Smarty
In the latest Smarty releases due to security settings PHP functions aren't available by default... If you do want to use PHP functions, you have to enable them by adding this line to your CMSMS config.php file:
This config variable loosens some of the security configuration for Smarty templates. Particularly enabling this option allows the use of any PHP function as a Smarty plugin. You better not use this option if you are allowing content to be submitted for display on your website from untrusted sources!
If the feed reader works without this line, you better not add it!
Working example
The next example is the output of the template described above and displays the CMS Can Be Simple blog feed:
CMS Made Simple
CMS Made Simple Announcements
Generator: CMS Made Simple
URL: https:https://www.cmsmadesimple.org/blog
CMSMS
Rolf Tjassens
31 May 2022
It is with great sadness that we announce long time Dev Team member and great friend to CMS Made Simple, Rolf Tjassens passed away unexpectedly on the 30th of May. Read more
v2.2.16 - Truro Release Announcement
24 January 2022
http://www.cmsmadesimple.org/downloads/cmsms Today we are delighted to announce the release of CMS Made Simple v2.2.16 - Truro. Truro is an incremental bug fix and security release. Read more
Comment Form
7 Comments
Hello!
I'm trying to get featured image to display in rssfeed - without much luck!
PHP file contains following:
public function hookDisplayFooter($params)
{
// Conf
$title = strval(Configuration::get('RSS_FEED_TITLE'));
$url = strval(Configuration::get('RSS_FEED_URL'));
$nb = (int) (Configuration::get('RSS_FEED_NBR')) ? (int) (Configuration::get('RSS_FEED_NBR')) : 5;
$cacheId = $this->getCacheId($this->name . '|' . date("YmdH"));
if (!$this->isCached($this->templateFile, $cacheId)) {
$rss_links = array();
if ($url && ($contents = Tools::file_get_contents($url))) {
try {
$xml = new SimpleXMLElement($contents);
$loop = 0;
if (!empty($xml->channel->item)) {
foreach ($xml->channel->item as $item) {
if (++$loop > $nb) {
break;
}
$rss_links[] = (array)$item;
}
}
} catch (Exception $e) {
Tools::dieOrLog($this->trans('Error: invalid RSS feed in "%module_name%" module: %message%', array('%module_name%' => $this->name, '%message%' => $e->getMessage()), 'Modules.Rssfeed.Admin'));
}
}
$this->smarty->assign(array(
'title' => ($title ? $title : $this->trans('RSS feed', array(), 'Modules.Rssfeed.Admin')),
'rss_links' => $rss_links
));
}
return $this->fetch($this->templateFile, $cacheId);
}
Smarty tpl file:
Latest Posts from our Temperature Blog
{if $rss_links}
{foreach from=$rss_links item='rss_link'}
{$rss_link.title}{$rss_link.description|truncate:150|escape:'UTF-8'}
{/foreach}
{else}
{l s='No RSS feed added'}
{/if}
What am I missing?
Thanks
Hello Vincent,
The photo's in that feed are included in the description/content field and not seperately attached. Your options are more limited by this.
Change the feed or contact the owner of that website and let him change it...
Grtz. Rolf
Hello thank's for this but i can't import the image how i can proceed if you have an idea ?
I want to import rss flux from this wordpress blog : http://blog.colvert.com
Any idea to help me please ?
Hi,
I i'm using this methode, but i have a problem with caching.
I can use your Cache remote file plugin for this but i don't seem to get it working.
Thank you, Joni!!
-------
I updated the post with a check if the feed is present and added the enclosure tag to retrieve the item image URL from the news feed.
Bravo, Rolf. This is exactly what I am looking for on a page for a client!!!