PHP template engine Smarty built-in function section, sectionelse Usage Details _ php instance

Source: Internet
Author: User
Tags php template
This article mainly introduces the usage of the Smarty built-in function section and sectionelse of the PHP template engine, and analyzes the techniques and usage of section and sectionelse in detail based on the instance form, for more information about how to use sectionelse, see the section of the Smarty built-in function of the PHP template engine. We will share this with you for your reference. The details are as follows:

Section is another loop Processing Solution in the Smarty template except foreach. section is more flexible than foreach, just like an improved foreach statement. Besides having the same loop characteristics, many additional options are provided to better control loop execution. In the template, you must use a pair of section tags. There are two attributes that must be set: name and loop. For details about section attributes, see the following table:

Attribute Type Required? Default Value Description
Name String Yes N/ Name of the loop
Loop [$ Variable_name] Yes N/ Variable name that determines the number of cycles
Start Integer No 0 The initial position of loop execution. if the value is negative, the start position is counted from the end of the array. for example, if there are 7 elements in the array and the value of start is-2, the index pointing to the current array is 5. the invalid value (beyond the lower limit of the loop array) is automatically adjusted to the closest legal value.
Step Integer No 1 This value determines the cycle step. for example, if step = 2 is specified, only elements with subscript 0, 2, and 4 will be traversed. if step is a negative value, traverse the array from the back to the front.
Max Integer No 1 Sets the maximum number of cycles.
Show Boolean No True Determines whether to display the loop.

We use an instance to demonstrate the use of {section} and {sectionelse} in Smarty.

Example: extract the content from the database, assign it to an array variable $ _ html, assign the array variable to the template, and then traverse the array in the template.

Database, main file index. php, Smarty template initialization file init. inc. php, refer to the previous article "PHP template engine Smarty built-in functions foreach, foreachelse Usage Analysis"

/Tpl/index. tpl

 Section, sectionelse 
 
 <{Section loop = $ data name = "ls" max = "100" start = "0" step = "2"}>    <{If $ smarty. section. ls. first}> 
   
     <{Elseif $ smarty. section. ls. last}> 
   
     <{Else}> 
   
     <{/If}> 
    <{Sectionelse}>  
   <{/Section }>< {if $ data}>  
   <{/If}> 
  
ID (iteration) Number (rownum) Name Email Time added
<{$ Smarty. section. ls. iteration}> <{$ Smarty. section. ls. rownum}> <{$ Data [ls]. username}> <{$ Data [ls]. email}> <{$ Data [ls]. addTime}>
Sorry! No data.
The number of cycles is: <{$ smarty. section. ls. total}>

Execution result:

Variables that can be used in the section circular area

Variable name

Description

Index Used to display the index of the current loop, starting from 0 (if the start attribute is set, it starts with this value), 1 is added each time (if the step attribute is specified, so it depends on the value)
Index_prev Used to display the index value of the previous loop. The value is-1 at the beginning of the loop.
Index_next This parameter is used to display the next loop index value. When the loop is executed until the last time, this value is still 1 greater than the current index value (if the step attribute is specified, it is determined by this value)
Iteration Used to display the number of cycles
First The value of this variable is true during the first execution of the current section loop.
Last The value of this variable is true during the last execution of the current section loop.
Rownum Used to display the number of cycles. This attribute is the alias of iteration. The two are the same.
Loop Used to display the index value of the last loop of the loop. This value can be used inside the loop or after the loop ends.
Show Is the section parameter. The show values are boolean values true and false. If it is set to false, this loop is not displayed. If the sectionelse clause is specified, whether or not the clause is displayed depends on the value.
Total Displays the number of times a loop is executed. This attribute can be called not only in the loop, but also after the execution ends.

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.