CMS Made Simple – Smarty Foreach Loops Made Simple

CMS Made Simple Basics – Foreach Loops

One of the most used programming features of CMS Made Simple is accessing data from the system’s core components and add-on modules. There are template tags available to access the most used information available from the core, like dynamic menus, page content, News articles, etc. Sometimes, the developer must extend the reach of a module’s default behaviour. And, most of the time, simple manipulations inside of templates will be sufficient.  This article will have a look at the basics behind accessing data from within a template using the foreach loop.

Information managed by the CMS is organized in groups of items or objects also known as collections. These collections are called, “arrays” in programming. The items/objects belonging to an array are found by looping through data using the foreach method. CMS Made Simple developers can access an array’s data by using foreach loops provided with the Smarty PHP Template Engine and directly via PHP inside of a User Defined Tag.

Let’s look at accessing data using the Smarty {foreach} loop inside of templates. The News module offers some good examples.

CMS Made Simple News Templates

Here’s a basic loop using a portion of the code from Sample Summary Template which will list each article title:

{foreach from=$items item=entry}
<li>{$entry->title}</li>
{/foreach}

Foreach loops can use if/then statements to show specific information. For example, a list of article titles published during 2014:

{foreach from=$items item=entry}
    {if $entry->postdate|cms_date_format:'%Y' == '2014'}
    <li>{$entry->title}</li>
    {/if}
{/foreach}

A list of news articles categorized by year is easily accomplished with more than one if/then statement.

<dl>
{foreach from=$items item=entry}
    <dt>2014</dt>
    {if $entry->postdate|cms_date_format:'%Y' == '2014'}
    <dl><a href="{$entry->moreurl}" title="{$entry->title|cms_escape:htmlall}">{$entry->title|cms_escape}</a></dl>
    {/if}

    <dt>2013</dt>
    {if $entry->postdate|cms_date_format:'%Y' == '2013'}
    <dl><a href="{$entry->moreurl}" title="{$entry->title|cms_escape:htmlall}">{$entry->title|cms_escape}</a></dl>
    {/if}

    <dt>2012</dt>
    {if $entry->postdate|cms_date_format:'%Y' == '2012'}
    <dl><a href="{$entry->moreurl}" title="{$entry->title|cms_escape:htmlall}">{$entry->title|cms_escape}</a></dl>
    {/if}
{/foreach}
</dl>

The Smarty foreach function has some good built-in helpers. For example, we can find the first and last iteration of a loop. This is helpful to present additional information or insert additional markup when printing the data.

{foreach from=$items item=entry name=articles}
    <li>{$entry->title}</li>
    {if $smarty.foreach.articles.last}
    <li>End of Article List</li>
    {/if}
{/foreach}

The above example prints additional information at the end of the list. Notice the “name” attribute in the foreach tag. It is needed to access Smarty foreach properties.

{foreach from=$items item=entry name=articles}
    {if $smarty.foreach.articles.first}
    <li>This is the first article: {$entry->title}</li>
    {/if}
{/foreach}

The above example prints only the first article. And to show the total number of foreach iterations printed, use the .total property:

{foreach from=$items item=entry name=articles}
    <li>{$entry->title}</li>

    {if $smarty.foreach.foo.last}
    <li>There are {$smarty.foreach.articles.total} articles.</li>
    {/if}
{/foreach}

The examples in this article show the Smarty version 2 foreach methods.  Look for my next article showing helpful advanced Smarty foreach methods and what changes were introduced in Smarty version 3 to make your CMS Made Simple site more dynamic and responsive to your project’s needs.  More information about the Smarty foreach loop can be found in the Smarty documentation.


Looking for quality CMS Made Simple Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , | Posted under CMS Made Simple | RSS 2.0

Author Spotlight

JohnnyB

JohnnyB

I've been a full-time front-end web developer for nine years working with design and marketing agencies and businesses located all over the USA. CMS Made Simple has been my go-to Content Management System since mid 2006.

3 Comments on CMS Made Simple – Smarty Foreach Loops Made Simple

  1. Tetsuo says:

    Nice and useful article, thank you for writing it :)

  2. Ben says:

    Very helpful article. But just one trick in my case :
    I tried to apply the categorized per year example, but I got stuck with the looping of the “foreach” statement. Here is my case :
    My news articles have one month limits (start/end), then only one per front-end is possible. That’s why I need list somewhere else a list of expired articles (archives). The problem is that when applying the above script, only the current article is listed (normally it shouldn’t). I know that there is an option for that in that goes with the {news} smarty, but I don’t know where to put it in the script, unless another solution is possible.
    Thank you very much for help.
    Ben

    • JohnnyB JohnnyB says:

      Hi Ben,
      I don’t think the categorized by year example will work since the articles use a start/end date. You might try the “showall” parameter to force them to show regardless if they have already expired. Please check the News module’s help link in the admin for more information.

Leave a Reply

Your email address will not be published. Required fields are marked *


− 3 = 4

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>