{section},{sectionelse}
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. |
show | boolean | No | true | determines whether or not to show this section |
Example 7-19. {section}
The above example will output:
Another couple of examples without an assigned array.
The above example will output:
|
Example 7-22. nested sections
The above example will output:
|
Example 7-23. sections and associative arrays
The above example will output:
Database example (eg using Pear or Adodb)
|
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 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
The above example will output:
|
index_prev
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
The above example will output a table containing the following:
|
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-27. {section} property iteration
The above example will output:
This example uses the iteration property to output a table header block every five rows (uses {if} with the mod operator).
|
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
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
The above example will output:
|
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
The above example will output:
|
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
The above example will output:
|
See also {foreach} and $smarty.section.