Template sections are used for looping over
arrays of data
(just like {foreach}). All
{section} tags must be paired with
{/section} tags. Required parameters are
name and loop. The name
of the {section} can be anything you like, made up of letters,
numbers and underscores. Sections can be nested, and the nested
section names must be unique from each other. The loop variable
(usually an array of values) determines the number of times the
section will loop. When printing a variable within a section, the
section name must be given next to variable name within brackets
[]. {sectionelse} is
executed when there are no values in the loop variable.
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.
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
id: {$custid[customer]}<br />
{/section}
<hr />
{* print out all the values of the $custid array reversed *}
{section name=foo loop=$custid step=-1}
{$custid[foo]}<br />
{/section}
$addr = array('253 N 45th', '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 example assumes that $custid, $name and $address are all
arrays containing the same number of values
*}
{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 N 45th
</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 7-21. {section} naming
{*
the name of the section can be anything you like,
as it is used to reference the data within the section
*}
{section name=anything loop=$custid}
<p>
id: {$custid[anything]}<br />
name: {$name[anything]}<br />
address: {$address[anything]}
</p>
{/section}
{*
sections can be nested as deep as you like. With nested sections,
you can access complex data structures, such as multi-dimensional
arrays. In this example, $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 />
{*
This is an example of printing an associative array
of data within a section
*}
{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>
{*
output database result in a 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>
{/section}
</table>
Example 7-24. {sectionelse}
{* sectionelse will execute if there are no $custid values *}
{section name=customer loop=$custid}
id: {$custid[customer]}<br />
{sectionelse}
there are no values in $custid.
{/section}
Note:
As of Smarty 1.5.0, the syntax for section property variables has
changed from {%sectionname.varname%} to
{$smarty.section.sectionname.varname}. The old syntax is still
supported, but you will only see examples of the new syntax.
index
index is used to display the current array index, starting with zero
(or the start attribute if given), and incrementing by one (or by
the step attribute if given.)
Technical Note:
If the step and start section properties are not
modified, then this works the same as the iteration section
property, except it starts at 0 instead of 1.
Example 7-25. {section} property index
{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
index_prev is used to display the previous loop index.
on the first loop, this is set to -1.
index_next
index_next is used to display the next loop index. On the last
loop, this is still one more than the current index (respecting the
setting of the step attribute, if given.)
Example 7-26. {section} property index_next and index_prev
iteration is used to display the current loop iteration.
Note:
This is not affected by the section properties start, step and
max, unlike the index
property. Iteration also starts with 1 instead of 0 like index. rownum is an alias to iteration,
they work identical.
Example 7-27. {section} property iteration
<?php
// array of 3000 to 3015 $id = range(3000,3015); $smarty->assign('custid',$id);
first is set to true if the current section iteration is the first
one.
last
last is set to true if the current section iteration is the last
one.
Example 7-28. {section} property first and last
This example loops the $customers array;
outputs a header block on the first iteration and
on the last outputs the footer block
(uses section total property)
rownum is used to display the current loop iteration,
starting with one. It is an alias to iteration, they work
identically.
loop
loop is used to display the last index number that this section
looped. This can be used inside or after the section.
Example 7-29. {section} property index
{section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
There were {$smarty.section.customer.loop} customers shown above.
The above example will output:
0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />
There were 3 customers shown above.
show
show is used as a parameter to section.
show is a boolean value, true or false. If
false, the section will not be displayed. If there is a {sectionelse}
present, that will be alternately displayed.
Example 7-30. {section} attribute show
{*
$show_customer_info (true/false) may have been passed from the PHP
application, to regulate whether or not this section shows
*}
{section name=customer loop=$custid show=$show_customer_info}
{$smarty.section.customer.rownum} id: {$custid[customer]}<br />
{/section}
{if $smarty.section.customer.show}
the section was shown.
{else}
the section was not shown.
{/if}
The above example will output:
1 id: 1000<br />
2 id: 1001<br />
3 id: 1002<br />
the section was shown.
total
total is used to display the number of iterations that this section
will loop. This can be used inside or after the section.
Example 7-31. {section} property total
{section name=customer loop=$custid step=2}
{$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}
There were {$smarty.section.customer.total} customers shown above.
The above example will output:
0 id: 1000<br />
2 id: 1002<br />
4 id: 1004<br />
There were 3 customers shown above.