PHP template engine smarty built-in function Section,sectionelse usage detailed _php example

Source: Internet
Author: User
Tags add time php template reserved first row smarty template

This example describes the PHP template engine smarty built-in function Section,sectionelse usage. Share to everyone for your reference, specific as follows:

The section is another way of dealing with loops besides foreach in the Smarty template, and section is more flexible than foreach, like an improved foreach statement that offers many additional options, in addition to having the same looping characteristics. Can better control the execution of the loop. In a template, you must use a pair of section tags, two property name and loop that must be set, and the properties for section see the following table:

Property 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 counted from the tail of the array. For example, if there are 7 elements in the array, and the specified start is-2, then the index to the current array is 5. An illegal value (exceeding the lower bound of the loop array) is automatically adjusted to the nearest legal value.
Step Integer No 1 This value determines the step size of the loop. For example, specifies that step=2 will only iterate over elements such as subscript 0, 2, 4, and so on. If step is negative, iterate through the array from the back forward.
Max Integer No 1 Sets the maximum number of times the loop executes.
Show Boolean No True Determines whether the loop is displayed.

We demonstrate the use of {section} and {Sectionelse} in Smarty by an instance.

Example idea: Take out the content from the database, assign to an array variable $_html, assign the array variable to the template, and then iterate through the array in the template.

Database, main file index.php,smarty template initialization file init.inc.php, refer to the previous "PHP template engine Smarty built-in function Foreach,foreachelse usage analysis"

/tpl/index.tpl

 

Execution results:

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 starting with that value), plus 1 each time (if the Step property is specified, it is determined by that value)
Index_prev Used to display the last circular index value, which is-1 when the loop begins
Index_next Used to display the next circular index value, which is still 1 larger than the current index value (if the Step property is specified, it is determined by the value) when the loop executes to the last time.
Iteration Number of times to display loops
The The value of the current section loop for the first execution of the variable is true
Last The value of the current section loop for the last execution of this variable is true
RowNum The number of times to display the loop, which is an alias for iteration, the same
Loop Used to display the index value of the last loop of the loop, which can be used for internal loops or after the end of the loop
Show is the argument for section, the show evaluates to Boolean True and false, and if set to False, the loop is not displayed. If the SECTIONELSE clause is specified, whether the clause is displayed or not depends on the value
Total The number of times to display the loop execution. This property can be called not only in loops, but also at the end of execution

More about PHP Interested readers can view the site topics: "Smarty Template Primer Tutorial", "PHP Template Technology Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on Smarty template.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.