Home >Backend Development >PHP Tutorial >Detailed usage of section loop in smarty

Detailed usage of section loop in smarty

ringa_lee
ringa_leeOriginal
2017-06-16 11:49:512069browse
The name of the looploop[$variable_name]Yesstart stepmaxshowbooleanNotrue


{section loop = $varName[, start = $start, step = $step, max = $max, show = true]}
name: The name of the section, not used Add $
$loop: the variable to be looped. Use assign to operate on this variable in the program.
$start: The subscript to start the loop, the loop subscript starts from 0 by default
$step: The increment of the subscript during each loop
$max: The maximum loop subscript
$show: boolean type , determine whether to display this block, the default is true

There is a noun that needs to be explained:
Loop subscript: Its actual English name is index, which means index. Here I will translate it It is called "subscript" mainly for easier understanding. It represents the loop index before

when displaying this loop block. It starts from 0 by default and is affected by $start. If $start is set to 5, it will also start counting from 5. In the template design We have used it in part. This is an attribute of the current

{section}. The calling method is Smarty.section.sectionName.index. The sectionName here refers to the name attribute in the function prototype.
{section} block has attribute values, respectively:
1. index: the "loop subscript" we introduced above, the default is 0
2. index_prev: the previous value of the current subscript, the default It is -1
3. index_next: the next value of the current index, the default is 1
4. first: whether it is the first cycle
5. last: whether it is the last cycle
6. iteration: the number of loops
7. rownum: the current row number, another alias of iteration
8. loop: the last loop number, which can be used to count the number of loops in the section after the section block
9. total: The number of loops can be used to count the number of loops after the section block
10. show: It is included in the function declaration and is used to determine whether the section is displayed
*foreach loop
1. foreach: used to loop simple arrays , it is a selective section loop, and its definition format is:
{foreach from=$array item=array_id}
{foreachelse}
{/foreach}
where, from indicates the The array variable to be looped, item is the name of the variable to be looped, and the number of loops is determined by the number of array variables specified by from. {foreachelse} is used to process when the array passed in the program is empty. The following is a simple example:
====================== =====================
example6.tpl
==================== =======================

<html>
<head><title>这是一个foreach使用的例子</title></head>
<body>
这里将输出一个数组:<br>
<{foreach from=$newsArray item=newsID}>
新闻编号:<{$newsID.newsID}><br>
新闻内容:<{$newsID.newsTitle}><br><hr>
<{foreachelse}>
对不起,数据库中没有新闻输出!
<{/foreach}>
</body>
</html>


Attribute Type Is it required? Default value Description
name string Yes ##n/a
##n /aThe variable name that determines the number of loops
integer No 0#The initial position of the loop execution. If the value is negative, the starting position is calculated from the end of the array. For example: If there are 7 items in the array element, specify start as -2, then the index pointing to the current array is 5. Illegal values ​​(exceeding the lower limit of the loop array) will be automatically adjusted to the closest legal value.
integer No 1This value determines the step size of the loop. For example, specify step=2 will only traverse elements with subscripts 0, 2, 4, etc. If step is a negative value, then traverse the array from back to front.
integer No 1##Set the maximum number of executions of the loop.
Determine whether to display the cycle.

The above is the detailed content of Detailed usage of section loop in smarty. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn