Displaying recursive datastructures with Smarty

03.08.2005 at 20:20

For those of you who don't know what smarty is, it is a PHP Templating-Engine whose main idea is to separate programm logic from presentation.

I would suggest reading the excellent documentation for a brief overview.

A quite common task in web development is to display recursive data structures such as tree menus etc. However this can be a bit tricky for someone new to smarty. There is currently no built in mechanism for doing so, but with the possibility to include subfiles it can be done. Here is how it works.

menu.tpl

{* other html stuff *}
<h2>Linklist by category</h2>
      {include file="menu-recursive.tpl" menu=$menu depth=1}
</div>
{* other html stuff *}

menu-recursive.tpl

{foreach name=entry item=entry from=$menu.entries}
    {if $smarty.foreach.entry.first}
        <ul>
    {/if}         
    <li>{$entry}</a></li>
    {if $smarty.foreach.entry.last}
        {if $menu.sub}
            {include file="menu-recursive.tpl" menu=$menu.sub depth=$depth+1}
        {/if}
        </ul>
    {/if}
{foreachelse}
    <p>This category contains currently no entries.</p>
{/foreach}

PHP-file with the datastructure.

$demo = array(
    'entries' => array('demo 1','demo 2'),
    'sub' => array(
        'entries' => array('demo 1.1','demo 2.1'),
        'sub' => array(
            'entries' => array('demo 1.1.1','demo 2.1.1')
        )
    )
);

$smarty->assign('menu',$demo);
$smarty->display('menu.tpl');

You should get a list looking similar to the one below.

However if a designer is able to understand this stuff is a completly different story...

Comments (2)

Jagged arrays with Smarty3

28.10.2013 by Vladimirs Malejevs (vladimirs16[at]inbox[dot]lv)

Jagged arrays index.php $menu_demo = array( 'entries' => array( array('href' => 'newest.php', 'text' => 'Jaun&#257;kie'), array('href' => 'popular.php', 'text' => 'Popular&#257;kie'), array('href' => 'comp.php', 'text' => 'Datortehnika', 'sub' => array( 'entries' => array( array('href'=>'portable_comp.php', 'text' => 'Portativie datori'), array('href'=>'desktop_comp.php', 'text' => 'Galddatori'), array('href'=>'comp_componenet.php', 'text' => 'Datoru komponentes', 'sub' => array( 'entries' => array( array('href' => 'cpu.php', 'text'=>'CPU'), array('href'=>'mb.php', 'text'=>'Pamatplates' ) ) ) ) ) ) ), array('href'=>'mobile.php', 'text'=>'Mobilie t&#257;lr'), array('href'=>'tourism.php', 'text'=>'Turizms', 'sub' =>array( 'entries'=>array( array('href'=>'knives.php', 'text'=>'Nai'), array('href'=>'ropes.php', 'text'=>'Virves') ) ) ) ) ); $smarty->assign('demo',$menu_demo); $smarty->display('index.tpl'); index.tpl <h2>Linklist by category</h2> {include file="menu-recursive.tpl" menu=$demo depth=1} menu-recursive.tpl {foreach $menu.entries as $val} {if $val@first} <ul> {/if} <li>{$val@index}. {$val@key}: <a href="{$val.href}">{$val.text}</a></li> {if $val.sub} {include file="menu-recursive.tpl" menu=$val.sub depth=$depth+1} {/if} {if $val@last} </ul> {/if} {foreachelse} <p>This category contains currently no entries.</p> {/foreach} Output: Linklist by category 0. 0: JaunÄkie 1. 1: PopularÄkie 2. 2: Datortehnika 0. 0: Portativie datori 1. 1: Galddatori 2. 2: Datoru komponentes 0. 0: CPU 1. 1: Pamatplates 3. 3: Mobilie tÄlr 4. 4: Turizms 0. 0: Naži 1. 1: Virves

you have a strange logic in example

17.01.2017 by Vladimir (trashboxvolodka[at]mail[dot]ru)

about {if $smarty.foreach.entry.last} why are you place including subnodes in this if statement? It should be before it. if subnodes will be in not last element you will have invalid output.

1