section,sectionelse
Template sections are used for looping over arrays of data. 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.
Example 7-15. section {* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
id: {$custid[customer]}<br>
{/section}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br> |
|
Example 7-16. section loop variable {* 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}
id: {$custid[customer]}<br>
name: {$name[customer]}<br>
address: {$address[customer]}<br>
<p>
{/section}
OUTPUT:
id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
<p> |
|
Example 7-17. section names {* the name of the section can be anything you like,
and it is used to reference the data within the section *}
{section name=mydata loop=$custid}
id: {$custid[mydata]}<br>
name: {$name[mydata]}<br>
address: {$address[mydata]}<br>
<p>
{/section} |
|
Example 7-18. nested sections {* 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}
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}
<p>
{/section}
OUTPUT:
id: 1000<br>
name: John Smith<br>
address: 253 N 45th<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: john@mydomain.com<br>
<p>
id: 1001<br>
name: Jack Jones<br>
address: 417 Mulberry ln<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<br>
<p>
id: 1002<br>
name: Jane Munson<br>
address: 5605 apple st<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<br>
<p> |
|
Example 7-19. sections and associative arrays {* This is an example of printing an associative array
of data within a section *}
{section name=customer loop=$contacts}
name: {$contacts[customer].name}<br>
home: {$contacts[customer].home}<br>
cell: {$contacts[customer].cell}<br>
e-mail: {$contacts[customer].email}<p>
{/section}
OUTPUT:
name: John Smith<br>
home: 555-555-5555<br>
cell: 555-555-5555<br>
e-mail: john@mydomain.com<p>
name: Jack Jones<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jack@mydomain.com<p>
name: Jane Munson<br>
home phone: 555-555-5555<br>
cell phone: 555-555-5555<br>
e-mail: jane@mydomain.com<p> |
|
Example 7-20. 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} |
|
Sections also have their own variables that handle section properties.
These are indicated like so: {$smarty.section.sectionname.varname}
Note:
As of Smarty 1.5.0, the syntax for section property variables has
been changed from {%sectionname.varname%} to
{$smarty.section.sectionname.varname}. The old syntax is still
supported, but you will only see reference to the new syntax in the
manual examples.
index
index is used to display the current loop 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 on 0 instead of 1.
Example 7-21. section property index {section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br>
{/section}
OUTPUT:
0 id: 1000<br>
1 id: 1001<br>
2 id: 1002<br> |
|
index_prev
index_prev is used to display the previous loop index.
on the first loop, this is set to -1.
Example 7-22. section property index_prev {section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br>
{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
{if $custid[customer.index_prev] ne $custid[customer.index]}
The customer id changed<br>
{/if}
{/section}
OUTPUT:
0 id: 1000<br>
The customer id changed<br>
1 id: 1001<br>
The customer id changed<br>
2 id: 1002<br>
The customer id changed<br> |
|
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-23. section property index_next {section name=customer loop=$custid}
{$smarty.section.customer.index} id: {$custid[customer]}<br>
{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
{if $custid[customer.index_next] ne $custid[customer.index]}
The customer id will change<br>
{/if}
{/section}
OUTPUT:
0 id: 1000<br>
The customer id will change<br>
1 id: 1001<br>
The customer id will change<br>
2 id: 1002<br>
The customer id will change<br> |
|
iteration
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-24. section property iteration {section name=customer loop=$custid start=5 step=2}
current loop iteration: {$smarty.section.customer.iteration}<br>
{$smarty.section.customer.index} id: {$custid[customer]}<br>
{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}
{if $custid[customer.index_next] ne $custid[customer.index]}
The customer id will change<br>
{/if}
{/section}
OUTPUT:
current loop iteration: 1
5 id: 1000<br>
The customer id will change<br>
current loop iteration: 2
7 id: 1001<br>
The customer id will change<br>
current loop iteration: 3
9 id: 1002<br>
The customer id will change<br> |
|
first
first is set to true if the current section iteration is the first
one.
Example 7-25. section property first {section name=customer loop=$custid}
{if $smarty.section.customer.first}
<table>
{/if}
<tr><td>{$smarty.section.customer.index} id:
{$custid[customer]}</td></tr>
{if $smarty.section.customer.last}
</table>
{/if}
{/section}
OUTPUT:
<table>
<tr><td>0 id: 1000</td></tr>
<tr><td>1 id: 1001</td></tr>
<tr><td>2 id: 1002</td></tr>
</table> |
|
last
last is set to true if the current section iteration is the last
one.
Example 7-26. section property last {section name=customer loop=$custid}
{if $smarty.section.customer.first}
<table>
{/if}
<tr><td>{$smarty.section.customer.index} id:
{$custid[customer]}</td></tr>
{if $smarty.section.customer.last}
</table>
{/if}
{/section}
OUTPUT:
<table>
<tr><td>0 id: 1000</td></tr>
<tr><td>1 id: 1001</td></tr>
<tr><td>2 id: 1002</td></tr>
</table> |
|
rownum
rownum is used to display the current loop iteration,
starting with one. It is an alias to iteration, they work
identically.
Example 7-27. section property rownum {section name=customer loop=$custid}
{$smarty.section.customer.rownum} id: {$custid[customer]}<br>
{/section}
OUTPUT:
1 id: 1000<br>
2 id: 1001<br>
3 id: 1002<br> |
|
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-28. 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.
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-29. section attribute show {* $show_customer_info 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}
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-30. 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.
OUTPUT:
0 id: 1000<br>
2 id: 1001<br>
4 id: 1002<br>
There were 3 customers shown above. |
|