PHP template engine Smarty built-in functions section,sectionelse usage, Smartysectionelse
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
<title>Section,sectionelse</title>
Number (iteration) |
number (rownum) |
name |
Email |
Add Time |
<{section loop= $data name= "ls" max= "start=" 0 "step=" 2 "}> <{if $smarty .section.ls.first}>
<{elseif $smarty .section.ls.last}>
<{else}>
<{/if}>
<{$smarty .section.ls.iteration}> |
<{$smarty .section.ls.rownum}> |
<{$data [ls].username}> |
<{$data [ls].email}> |
<{$data [ls].addtime}> |
<{sectionelse}>
I am sorry! No data for the time being. |
<{/section}> <{if $data}>
The number of cycles is: <{$smarty .section.ls.total}> |
<{/if}>
Execution Result:
Variables that can be used in the section loop area
Variable name |
Description |
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 |
More about PHP related content readers can view the topic: "Smarty Template Primer Basic Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " PHP Basic Grammar Introductory tutorial, PHP Object-oriented Programming primer, PHP string usage Summary, PHP+MYSQL database Operations Primer and PHP Common database operations Tips Summary
It is hoped that this article will be helpful to everyone based on smarty template PHP program design.
Articles you may be interested in:
- PHP template engine Smarty built-in functions
- PHP template engine Smarty built-in variable mediator usage
- PHP template engine smarty custom variable mediator usage
- Analysis of the usage of reserved variables in PHP template engine Smarty
- PHP template engine Smarty built-in function foreach,foreachelse usage analysis
- PHP template Engine Smarty Example of how a configuration file is used in template variables
- Example of how variables are used in PHP template engine Smarty
- Smarty how the template engine obtains data from PHP
- thinkphp How to use the Smarty template engine
- A method of generating random numbers in the PHP template engine smarty and an explanation of the math function
- Summary of cache Usage for PHP template engine Smarty
- 6 Tips for PHP smarty template engine
- [PHP] Template engine Smarty Introduction
http://www.bkjia.com/PHPjc/1119970.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119970.html techarticle PHP template engine smarty built-in functions section,sectionelse usage, Smartysectionelse This example describes the PHP template engine smarty built-in functions section,sectionelse usage. Share to everyone ...