While the {foreach}
function is perfect for handling PHP associative arrays, the {section}
function is aimed at processing deeply nested arrays, or normal indexed arrays. Let’s expand our page that describes the management team of our company by passing a deeply nested array describing each of the team members. This can be done by following these steps:
index.php
to the following:<?php include_once(‘libs/Smarty.class.php’); $smarty = new Smarty; if (empty($_GET[‘language’])) { $smarty->assign(‘language’, ‘english’); } else { if ($_GET[‘language’] == ‘en’) { $smarty->assign(‘language’, ‘english’); } elseif ($_GET[‘language’] == ‘it’) { $smarty->assign(‘language’, ‘italian’); } } $team = array( 0 => array( ‘name’ => ‘Julian Fox’, // crazy like a fox! ‘title’ => ‘CEO’, ‘description’ => ‘Julian handles all management issues related to the company, such as dealing with potential customers and providing a clear strategy for services.’, ), 1 => array( ‘name’ => ‘Ryan Foster’, ‘title’ => ‘CTO’, ‘description’ => ‘Ryan handles all technical aspects of consulting services available through Smarty LLC’, ), 2 => array( ‘name’ => ‘Adam Salsbury’, // he will sell you anything! ‘title’ => ‘VP Sales’, ‘description’ => ‘Adam is the person who closes our consulting deals and handles all interactions with existing customers.’, ), ); $smarty->assign(‘team’, $team); // list of available languages $languages = array( ‘en’ => ‘English’, ‘it’ => ‘Italian’, ‘de’ => ‘German’, ‘pt’ => ‘Portuguese’ ); $smarty->assign(‘languages’, $languages); $smarty->display(‘about.tpl’); ?>
about.tpl
template file to the following:{config_load file=”$language.conf” section=”About”} {include file=”header.tpl”} {include file=”navigation.tpl” page_title=”Management Team”} {section name=”i” loop=$team} <p><b>{$team[i].name}, {$team[i].title}</b> - {$team[i].description}</p> {/section} {include file=”footer.tpl”}
Voilà! You are now dynamically displaying the list of employees based on whatever you pass from the PHP script. The {section}
function is similar to PHP’s for
construct, in that it loops through an array, and you can treat each of the elements in the array as an associative array. In our example above, we are looping through the list of employees, and using each key in the associative array to display the details of the employee, such as name, title, and description of what they do in the company.
You may also utilize most of the attributes available for the {foreach}
function, such as last
(as exemplified before) and more:
start
: This allows you to start iterating the given array from the specified position (the first element is zero).step
: This allows you to set a custom step value, and skip certain values. For instance, if you set step
to 3
, the loop will process records 0, 3, 6 and so on.max
: The maximum number of times that this loop will iterate.show
: Whether to display the output of this function or not.index
: Displays the current iteration, starting from zero.index_prev
: Displays the previous iteration index, if any. It is set to -1
on the first iteration of the loop.index_next
: Displays the next iteration index.iteration
: Displays the current iteration index, starting from one.first
: Set to true if the current iteration is the first one of the loop.last
: Set to true if the current iteration is the last one of the loop.However, instead of using $smarty.foreach.foreach_name.last
, you will need to use $smarty.section.section_name.last
, and so on. In order to better exemplify their usage, let’s modify our management team template to display a number before the name of each employee.
Update the about.tpl
file to contain the following:
{config_load file=”$language.conf” section=”About”} {include file=”header.tpl”} {include file=”navigation.tpl” page_title=”Management Team”} {section name=”i” loop=$team} <p><b>{$team[i].name}, {$team[i].title}</b> - {$team[i].description}</p> {/section} {include file=”footer.tpl”}
Reload the page in your browser, and you should see something like this:
3.139.233.43