A {section} is for looping over 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.
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 |
Required attributes are name and loop.
The name of 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 loop attribute, 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} name must 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 attribute name.
{section} properties are index, index_prev, index_next, iteration, first, last, rownum, loop, show, total.
Example 7-30. Looping a simple array with {section} assign() an array to Smarty
The template that outputs the array
The above example will output:
|
Example 7-32. 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}.
|
Example 7-33. 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.
The template to output $contacts
The above example will output:
|
Example 7-35. 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.
In this template, $contact_type[customer] is an array of contact types for the current customer.
The above example will output:
|
Example 7-36. Database example with a {sectionelse} Results of a database search (eg ADODB or PEAR) are assigned to Smarty
The template to output the database result in a HTML table
|
index contains the current array index, starting with zero or the start attribute if given. It increments by one or by the step attribute if given.
Technical Note: If the step and start properties are not modified, then this works the same as the iteration property, except it starts at zero instead of one.
index_next is 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-38. index, index_next and index_prev properties
Template to output the above array in a table
The above example will output a table containing the following:
|
iteration contains the current loop iteration and starts at one.
Note: This is not affected by the {section} properties start, step and max, unlike the index property. iteration also starts with one instead of zero unlike index. rownum is an alias to iteration, they are identical.
Example 7-39. A section's iteration property
Template to output every other element of the $arr array as step=2
The above example will output:
Another example that uses the iteration property to output a table header block every five rows. Uses the {if} function with the mod operator.
|
last is set to TRUE if the current section iteration is the final one.
Example 7-40. {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. Also uses the total property.
|
rownum contains the current loop iteration, starting with one. It is an alias to iteration, they work identically.
loop contains the last index number that this {section} looped. This can be used inside or after the {section}.
Example 7-41. {section} property loop
The above example will output:
|
show is used as a parameter to section and is a boolean value. If FALSE, the section will not be displayed. If there is a {sectionelse} present, that will be alternately displayed.
Example 7-42. show property Boolean $show_customer_info has been passed from the PHP application, to regulate whether or not this section shows.
The above example will output:
|
total contains the number of iterations that this {section} will loop. This can be used inside or after a {section}.
See also {foreach} and $smarty.section.