A XML or RSS-feed reader with plain Smarty

  Previous article Next article  

A XML or RSS-feed reader with plain Smarty

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.3.1
  Last updated: 14-01-2018
  Comments: 0
  http://cms.ms/mMW2

Create 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.

{$xml = simplexml_load_file('https://www.cmscanbesimple.org/feeds/blog.rss')}

{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->title}
{$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->link}
{$item->title}
{$item->pubDate}
{$item->author}
{$item->description}
{$item->guid}

{$item->enclosure['url']}
{$item->enclosure['length']}
{$item->enclosure['type']}

Options

Channel image example

{if isset($xml->channel->image->url)}
  <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

{if isset($item->enclosure['url'])}
  <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:

$config['permissive_smarty'] = 1;

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 Can Be Simple.org

Tips, tricks and tutorials to make it even easier to work with CMSMS™

Generator:
URL: https://www.cmscanbesimple.org/blog
Copyright CMS Can Be Simple.org

Next-page button for the Gbook module

29 November 2017
Show guestbook replies from the Gbook module for CMSMS with "endless scrolling" instead of a paginated view. Read more

How to use SitemapMgr

20 May 2017
SitemapMgr creates page and module sitemap, robots.txt and humans.txt files. Ready to use sitemaps for several modules like News, CGBlog and Products are included! Read more






CMS Made Simple 2.2.5 - Tutorials, tips and tricks - CMSMS 2.2.5

A XML or RSS-feed reader with plain Smarty

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.3.1
  Last updated: 14-01-2018
  Comments: 0
  http://cms.ms/mMW2