Use of section in smarty

Source: Internet
Author: User

In the process of using smarty, there are many times that an array needs to be output to the template for processing. The following shows how to associate an index array with assocaite) the array is displayed on the page.

This document assumes that the following index array is used.

1. Index Array

1 $ people = array ('Tony ', 'sweety', 'abc', 'four ');
2 $ smarty-> assign ('people', $ people); displayed in the template: 1 {section name = n loop = $ people}
2 Name: {$ people [N]} <br/>
3 {/section}

In this way, the content of this array can be displayed in the template. The result is as follows:

1 Name: Tony
2 Name: Sweety
3 name: ABC
4 name: Four

 

 

2. Associate an array

1 $ arr = array (
2 array ('id' => 1, 'title' => 'title1 '),
3 array ('id' => 2, 'title' => 'title2 '),
4 array ('id' => 3, 'title' => 'title3 ')
5 );
6
7 $ smarty-> assign ('News', $ ARR );

 

The display process in the template is as follows:

1 {section name = Sn loop = $ news}
2 {if $ smarty. Section. Sn. First}
3 <Table>
4 <TH> id </Th>
5 <TH> title </Th>
6 {/if}
7 <tr>
8 <TD >{$ news [Sn]. ID} </TD>
9 <TD >{$ news [Sn]. Title} </TD>
10 </tr>
11 {if $ smarty. Section. Sn. Last}
12 </table>
13 {/if}
14 {sectionelse}
15 there is no news.
16 {/section}

 

The result is as follows (a table is displayed, and no style is added as follows ):

1 ID title
2 1 title1
3 2 title2
4 3 title3

 

It can be seen that indexes and associated arrays are easy to use. The following describes the attributes in the Section:

1. attributes in Section

Name: (required) The section loop name only indicates the unique name of the loop. There is no $ symbol before it;

Loop: (required) is the name of the variable in the PHP declaration. It is used to indicate which array to loop (that is, the name of the array to loop) requires $;

Start: (optional) 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: (optional) for example, a loop in other languages. It is a step. If it is a negative number, it is a reverse loop;

MAX: (optional) Maximum subscript of a loop. If it is 1, it will only loop once. If it is 2, it will loop twice;

Show: (optional) The default value is true. If {sectionelse} is set }. It indicates that this part of content is displayed when the array has no content; if show is false, this part is displayed. If {sectionelse} is not set, this array is not output.

 

2. Variables in section in smarty

Index: used to display the index of the current loop, starting from 0 (if the start attribute is specified, it starts with this value), and 1 is added each time (if the step attribute is specified, determined by this value ). if the step and start attributes are not specified, the function of this value is similar to that of iteration, but it starts from 0.

Index_prev: used to display the index value of the previous loop. The value is-1 at the beginning of the loop.

Index_next: used to display the value of the next loop index. When the loop is executed until the last time, this value is still 1 greater than the current index value (if step is specified, it depends on this value ).

Iteration: used to display the number of cycles. unlike the index attribute, which is affected by the start, step, and Max attributes, iteration always starts from 1 (index starts from 0 ). rownum is the alias of iteration.

First: if the current loop is executed for the first time, first is set to true.

Last: if the current loop is executed until the last time, the last value is set to true.

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 or after the loop ends.

Show: is a section parameter.ShowThe value is a Boolean value of true or false. If it is set to false, the loop will not be displayed. If the sectionelse clause is specified, whether the statement is displayed depends on the value.

Total: displays the total number of execution cycles. You can call this attribute in a loop or after execution ends.

Use of section in smarty

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.