This article mainly introduces the PHP template engine smarty built-in function section,sectionelse usage, combined with the case form detailed analysis of the Section,sectionelse loop processing techniques and the use of methods, the need for friends can refer to the following
The example in this article describes the Section,sectionelse usage of the PHP template engine smarty built-in functions. Share to everyone for your reference, as follows:
section is a scheme for processing loops in the Smarty template in addition to foreach, and the section is more flexible than foreach, like an improved foreach statement that provides many additional options in addition to having the same looping characteristics. Can better control the execution of the loop. In the template, you must use paired section tags, there are two properties that must be set for name and loop, and for the properties of the section, see the following table:
Properties |
type |
whether you must |
Default Value |
Description |
Name |
String |
Yes |
N/A |
The name of the loop |
Loop |
[$variable _name] |
Yes |
N/A |
Variable name that determines the number of cycles |
Start |
Integer |
No |
0 |
The initial position of the loop execution. If the value is negative, the start position is calculated from the end of the array. For example, if there are 7 elements in the array, specifying start IS-2, then the index to the current array is 5. Illegal values (exceeding the lower bounds of the loop array) are automatically adjusted to the nearest legal value. |
Step |
Integer |
No |
1 |
This value determines the step size of the loop. For example, specifying step=2 will only traverse elements that are labeled 0, 2, 4, and so on. If step is negative, then traverse the array from backward to forward. |
Max |
Integer |
No |
1 |
Sets the maximum number of cycles to execute. |
Show |
Boolean |
No |
True |
Determines whether the loop is displayed. |
We demonstrate the use of {section} and {Sectionelse} in Smarty by an example.
Example idea: Take out the contents from the database, assign an array variable $_html, assign the array variable to the template, and then iterate over the array in the template.
Database, main file index.php,smarty template initialization file init.inc.php, can refer to the previous "PHP template engine Smarty built-in function foreach,foreachelse usage analysis"
/tpl/index.tpl
Execution Result:
Variables that can be used in the section loop area
Variable name |
Describe |
Index |
The index used to display the current loop, starting at 0 (if the Start property is set, then the value starts), plus 1 per time (if the Step property is specified, the value is determined) |
Index_prev |
Used to display the previous loop index value, which is 1 when the loop starts |
Index_next |
Used to display the next loop index value, which is still 1 larger than the current index value when the loop executes to the last time (this value is determined if the Step property is specified) |
Iteration |
Number of times to display loops |
First |
The value of this variable is true at the time of the first execution of the current section loop |
Last |
The value of the current section loop at the last execution of the variable is true |
RowNum |
The number of times to display the loop, which is the alias of the iteration, the same |
Loop |
The index value that is used to display the last loop of the loop, which can be used inside the loop or after the loop has ended |
Show |
is a section parameter, show evaluates to a Boolean value of True and false, and if set to False, the loop will not be displayed. If the SECTIONELSE clause is specified, whether the clause is displayed or not depends on the value |
Total |
Used to show the number of times the loop was executed. This property can be called not only in the loop, but also at the end of execution |
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!