Section and foreach in smarty

Source: Internet
Author: User

Section attributes
The section is designed to solve the problem of foreach. Like foreach, it is used to design the loop blocks in the template. It is complex and can meet the needs of the program greatly, so I am used to using it in the program instead of using foreach. The basic reason is:
{Section name = name loop = $ varName [, start = $ start, step = $ step, max = $ max, show = true]}
Name: section name, no need to add $
$ Loop: The variable to be recycled. in the program, use assign to operate the variable.
$ Start: subscript of the start loop. The subscript of the loop starts from 0 by default.
$ Step: increment of the mark in each loop
$ Max: Maximum loop subscript

$ Show: boolean type. determines whether to display this block. The default value is true.

Here is a glossary:
Cycle subscript: Actually, its English name is index, which means index. Here I translate it into "subscript" for better understanding. It indicates the current loop index when the loop block is displayed. The default value starts from 0 and is affected by $ start. If $ start is set to 5, it will also count from 5, we used it in the template design section. This is an attribute of the current {section} and the call method is Smarty. section. sectionName. index. Here, sectionName refers to the name attribute in the function prototype.
The attribute values of the {section} block are:
1. index: the "loop subscript" introduced above. The default value is 0.
2. index_prev: the first value of the current underlying object. The default value is-1.
3. index_next: The next value of the current underlying object. The default value is 1.
4. first: whether it is the first next loop
5. last: whether it is the last loop
6. iteration: number of cycles
7. rownum: current row number, another alias of iteration
8. loop: the last cycle number, which can be used to count the number of cycles of a section after the section Block.
9. total: number of cycles. The number of cycles can be counted after the section.

10. show: it is included in the function declaration to determine whether the section is displayed.

Attributes of foreach
{Foreach} is used to cyclically access an associated array like a digital index array. It is different from the {section} That can only access the numeric index array, the syntax of {foreach} is much simpler than that of {section}, but it can only be used as a compromise for a single array. Each {foreach} tag must be paired with the close tag {/foreach.
Attribute
1. from type: array necessity: Yes required default value: n/a description: cyclically accessed array
2. item type: string necessity: Yes required default value: n/a description: variable name of the current element
3. key type: string necessity: No optional default value: n/a description: variable name of the current key name

4. name type: string necessity: No optional default value: n/a description: name of the foreach loop used to access the foreach attribute

From and item are required attributes
The {foreach} loop name can be a combination of any letters, arrays, and underscores. For more information, see PHP variables.
{Foreach} loops can be nested. The nested {foreach} names should be different.
The from attribute is usually a value array and is used to determine the number of {foreach} cycles.
If no value exists in the from variable, {foreachelse} is executed }.
The {foreach} loop also has its own attribute variables, which can be accessed through {$ smarty. foreach. name. property}, where "name" is the name attribute.
Note: The name attribute is valid only when you need to access the {foreach} attribute. It is different from {section. Accessing the {foreach} attribute of an undefined name will not throw an error, but it will lead to unpredictable results.
{Foreach} attributes include index, iteration, first, last, show, total.
 
{$ Smarty. foreach. name. index} // subscript
{$ Smarty. foreach. name. iteration} // subscript + 1
{$ Smarty. foreach. name. total} // total

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.