A {section}
is for looping over sequentially indexed arrays of data,
unlike {foreach}
which is used to loop over a
single associative array.
Every {section} tag must be paired with
a closing {/section} tag.
Note
The {foreach} loop can do everything a {section} loop can do, and has a simpler and easier syntax. It is usually preferred over the {section} loop.
Note
{section} loops cannot loop over associative arrays, they must be numerically indexed, and sequential (0,1,2,...). For associative arrays, use the {foreach} loop.
| Attribute Name | Type | Required | Default | Description |
|---|---|---|---|---|
| name | string | Yes | n/a | The name of the section |
| loop | mixed | Yes | n/a | Value to determine the number of loop iterations |
| start | integer | No | 0 | The index position that the section will begin looping. If the value is negative, the start position is calculated from the end of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values (values outside of the length of the loop array) are automatically truncated to the closest valid value. |
| step | integer | No | 1 | The step value that will be used to traverse the loop array. For example, step=2 will loop on index 0,2,4, etc. If step is negative, it will step through the array backwards. |
| max | integer | No | n/a | Sets the maximum number of times the section will loop. |
| show | boolean | No | TRUE |
Determines whether or not to show this section |
Option Flags:
| Name | Description |
|---|---|
| nocache | Disables caching of the {section} loop |
Required attributes are
nameandloop.The
nameof the{section}can be anything you like, made up of letters, numbers and underscores, like PHP variables.{section}'s can be nested, and the nested
{section}names must be unique from each other.The
loopattribute, usually an array of values, determines the number of times the{section}will loop. You can also pass an integer as the loop value.When printing a variable within a
{section}, the{section}namemust be given next to variable name within [brackets].{sectionelse}is executed when there are no values in the loop variable.A
{section}also has its own variables that handle{section}properties. These properties are accessible as:{$smarty.section.name.property}where “name” is the attributename.{section}properties areindex,index_prev,index_next,iteration,first,last,rownum,loop,show,total.
Example 112. Looping a simple array with {section}
assign() an array to Smarty
<?php
$data = array(1000,1001,1002);
$smarty->assign('custid',$data);
?>
The template that outputs the array
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
{section customer $custid} {* short-hand *}
id: {$custid[customer]}<br />
{/section}
<hr />
{* print out all the values of the $custid array reversed *}
{section name=foo loop=$custid step=-1}
{section foo $custid step=-1} {* short-hand *}
{$custid[foo]}<br />
{/section}
The above example will output:
id: 1000<br /> id: 1001<br /> id: 1002<br /> <hr /> id: 1002<br /> id: 1001<br /> id: 1000<br />
Example 113. {section} without an assigned array
{section name=foo start=10 loop=20 step=2}
{$smarty.section.foo.index}
{/section}
<hr />
{section name=bar loop=21 max=6 step=-2}
{$smarty.section.bar.index}
{/section}
The above example will output:
10 12 14 16 18 <hr /> 20 18 16 14 12 10
Example 114. Naming a {section}
The name of the {section} can be anything
you like, see PHP variables.
It is used to reference the data within the {section}.
{section name=anything loop=$myArray}
{$myArray[anything].foo}
{$name[anything]}
{$address[anything].bar}
{/section}
Example 115. Looping an associative array with {section}
This is an example of printing an associative array
of data with a {section}. Following is the php script to assign the
$contacts array to Smarty.
<?php
$data = array(
array('name' => 'John Smith', 'home' => '555-555-5555',
'cell' => '666-555-5555', 'email' => 'john@myexample.com'),
array('name' => 'Jack Jones', 'home' => '777-555-5555',
'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),
array('name' => 'Jane Munson', 'home' => '000-555-5555',
'cell' => '123456', 'email' => 'jane@myexample.com')
);
$smarty->assign('contacts',$data);
?>
The template to output $contacts
{section name=customer loop=$contacts}
<p>
name: {$contacts[customer].name}<br />
home: {$contacts[customer].home}<br />
cell: {$contacts[customer].cell}<br />
e-mail: {$contacts[customer].email}
</p>
{/section}
The above example will output:
<p> name: John Smith<br /> home: 555-555-5555<br /> cell: 666-555-5555<br /> e-mail: john@myexample.com </p> <p> name: Jack Jones<br /> home phone: 777-555-5555<br /> cell phone: 888-555-5555<br /> e-mail: jack@myexample.com </p> <p> name: Jane Munson<br /> home phone: 000-555-5555<br /> cell phone: 123456<br /> e-mail: jane@myexample.com </p>
Example 116. {section} demonstrating the loop variable
This example assumes that $custid, $name
and $address are all
arrays containing the same number of values. First the php script that assign's the
arrays to Smarty.
<?php
$id = array(1001,1002,1003);
$smarty->assign('custid',$id);
$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);
$addr = array('253 Abbey road', '417 Mulberry ln', '5605 apple st');
$smarty->assign('address',$addr);
?>
The loop variable only determines the number of times to loop.
You can access ANY variable from the template within the {section}.
This is useful for looping multiple arrays. You can pass an array which will determine
the loop count by the array size, or you can pass an integer to specify the number of loops.
{section name=customer loop=$custid}
<p>
id: {$custid[customer]}<br />
name: {$name[customer]}<br />
address: {$address[customer]}
</p>
{/section}
The above example will output:
<p> id: 1000<br /> name: John Smith<br /> address: 253 Abbey road </p> <p> id: 1001<br /> name: Jack Jones<br /> address: 417 Mulberry ln </p> <p> id: 1002<br /> name: Jane Munson<br /> address: 5605 apple st </p>
Example 117. Nested {section}'s
{section}'s can be nested as deep as you like. With nested {section}'s,
you can access complex data structures, such as multi-dimensional
arrays. This is an example .php script thats assign's the arrays.
<?php
$id = array(1001,1002,1003);
$smarty->assign('custid',$id);
$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);
$addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');
$smarty->assign('address',$addr);
$types = array(
array( 'home phone', 'cell phone', 'e-mail'),
array( 'home phone', 'web'),
array( 'cell phone')
);
$smarty->assign('contact_type', $types);
$info = array(
array('555-555-5555', '666-555-5555', 'john@myexample.com'),
array( '123-456-4', 'www.example.com'),
array( '0457878')
);
$smarty->assign('contact_info', $info);
?>
In this template, $contact_type[customer] is an array of contact types for the current customer.
{section name=customer loop=$custid}
<hr>
id: {$custid[customer]}<br />
name: {$name[customer]}<br />
address: {$address[customer]}<br />
{section name=contact loop=$contact_type[customer]}
{$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br />
{/section}
{/section}
The above example will output:
<hr>
id: 1000<br />
name: John Smith<br />
address: 253 N 45th<br />
home phone: 555-555-5555<br />
cell phone: 666-555-5555<br />
e-mail: john@myexample.com<br />
<hr>
id: 1001<br />
name: Jack Jones<br />
address: 417 Mulberry ln<br />
home phone: 123-456-4<br />
web: www.example.com<br />
<hr>
id: 1002<br />
name: Jane Munson<br />
address: 5605 apple st<br />
cell phone: 0457878<br />
Example 118. Database example with a {sectionelse}
Results of a database search (eg ADODB or PEAR) are assigned to Smarty
<?php
$sql = 'select id, name, home, cell, email from contacts '
."where name like '$foo%' ";
$smarty->assign('contacts', $db->getAll($sql));
?>
The template to output the database result in a HTML table
<table>
<tr><th> </th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
{section name=co loop=$contacts}
<tr>
<td><a href="view.php?id={$contacts[co].id}">view<a></td>
<td>{$contacts[co].name}</td>
<td>{$contacts[co].home}</td>
<td>{$contacts[co].cell}</td>
<td>{$contacts[co].email}</td>
<tr>
{sectionelse}
<tr><td colspan="5">No items found</td></tr>
{/section}
</table>




