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:https://www.cmsmadesimple.org/blog
CMSMS

2.2.20 In Review

17 June 2024
In an ongoing effort to improve transparency of the Dev Team, we'd like to take this opportunity to discuss a few of the issues that arose during and after the release of CMSMS 2.2.20. Read more

CMS Made Simple 2.2.21 - Sherbrooke

6 June 2024
Hello CMSMSers! Today we are announcing the release of CMS Made Simple 2.2.21 - Sherbrooke, an incremental bug fix release. This version addresses a few bugs that came up after the last release: template inheritance was broken for content blocks; the page editor didn't show the text area field for content blocks on some configurations when template inheritance was used; the page... Read more




Buy Me A Coffee


  Comment Form

ReviewManager

Click here to open the form

ReviewManager

  7 Comments

Buy Me A Coffee

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: 25-03-2021
  Comments: 7
  http://cms.ms/mMW2


Buy Me A Coffee




Advertisement


Ads help me to help you! Thanks!

Ads help me to help you! Buy products from these advertisers!