MENU

A XML or RSS-feed reader with plain Smarty

  Previous article Next article  

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 Made Simple

CMS Made Simple Announcements

Generator: CMS Made Simple
URL: https://www.cmsmadesimple.org/blog
CMSMS

Announcing CMSMS 2.2.8 - Flin Flon

28 July 2018
Today, we are happy to announce that we have released an incremental improvement to your favorite content management system: Version 2.2.8 - Flin Flon. The primary change in this version is to support PHP 7.2 by cleaning up the code to remove most of the warnings and notices due to the changes in PHP. Read more

New Video from the CMSMS Dev Team

3 April 2018

Read more





  Comment Form

Click here to open the form


  5 Comments

CMS Made Simple - Tutorials, tips and tricks - CMSMS

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: 11-02-2018
  Comments: 5
  http://cms.ms/mMW2

Advertisement



Ads help me to help you! Thanks!

Ads help me to help you! Thanks!

Ads help me to help you! Thanks!