There are two ways of data looping in the PHP smarty template engine, namely foreach and section loops. The following is an introduction to these two loop methods one by one.
(1) smart foreach loop
foreach is used to loop through an associative array like a numeric index array. Unlike section, which can only access numeric index arrays, the syntax of foreach is much simpler than that of section, but as a compromise it can only for a single array. Each foreach tag must be paired with a closing tag/foreach.
The foreach loop has four attributes, from, item, name, key, among which from and item are required.
Foreach loops can be nested, and the names of nested foreach should be different from each other.
When the from attribute has no value, the {foreachelse} fragment will be executed.
The foreach loop also has variables with its own attributes, which can be accessed through {$smarty.foreach.name.property}, where "name" is the name attribute. For example:
$smarty.foreach.name.index current array index, starting from zero.
$smarty.foreach.name.iteration The current number of loops, different from index, starts from 1 and increases by 1 each time it loops.
$smarty.foreach.name.first evaluates to TRUE when the current {foreach} loop is at the initial position.
$smarty.foreach.name.last is TRUE when the current {foreach} loop is at the final position.
$smarty.foreach.name.show show is the parameter of {foreach}. show is a Boolean value. If the value is FALSE, {foreach} will not be displayed. If there is a corresponding {foreachelse}, it will be displayed.
$smarty.foreach.name.total total includes the number of times {foreach} will loop, and can be used either within {foreach} or after.
Note: The name attribute is only valid when accessing the {foreach} attribute, which is different from {section}. Accessing the {foreach} attribute of undefined name will not throw an error, but will lead to unpredictable results.
Example:
{foreach from=$newsList item=news key=newsId name=news}
php smarty foreach loop example
(2) smart section loop
{section name="sn" loop="$BusinesList4"}
It can be seen that both index and associative array are very convenient to use. The following introduces the description of each attribute in the section:
1. Attributes in smart section
name: (required) is the name of the section loop. It just indicates the unique name of the loop. It has no special meaning. There is no $ sign in front;
loop: (required) is the variable name in the PHP declaration. It is used to indicate which array to loop (that is, the array name to be looped). You need to use $;
start: (optional) The initial position of loop execution. If the value is a negative number, the starting position is calculated from the end of the array. For example: if there are 7 elements in the array, specify start as -2, then point to the current array The index is 5. Illegal values (exceeding the lower limit of the loop array) will be automatically adjusted to the closest legal value.
step: (optional) Like loops in other languages, it is a step size. If it is a negative number, it will loop in reverse order;
max: (optional) The maximum subscript of the loop. If it is 1, it will only loop once. If it is 2, it will loop twice;
show: (optional) defaults to true and is displayed. If {sectionelse} is set. Indicates that if the array has no content, the content of this part will be displayed; if show is false, this part will be displayed. If {sectionelse} is not set, the array will not be 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, then it starts from this value), and increases by 1 each time (if the step attribute is specified, then it is determined by this value). If not Specify the step and start attributes. The function of this value is similar to iteration, except that it starts from 0.
index_prev: used to display the previous loop index value. When the loop starts, this value is -1.
index_next: used to display the next loop index value. When the loop is executed for 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 loops. Unlike the index attribute, which is affected by the start, step and max attributes, the value always starts from 1 (index starts from 0). rownum is an alias of iteration, two are equivalent.
first: If the current loop is executed for the first time, first is set to true.
last: If the current loop is executed for the last time, last is set to true.
rownum: used to display the number of loops. This attribute is an alias of iteration, and the two are equivalent.
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 a parameter of section. The value of show is a Boolean value true or false. If set to false, the loop will not be displayed. If the sectionelse clause is specified, whether the clause is displayed also depends on this value.
total: used to display the total number of loop executions. This attribute can be called during the loop or after execution.
Simple example:
{section name=loop loop=6}
id: {$smarty.section.loop.index+1}
{/section}
smarty section nested loop example:
{section name=”sn” loop=”$dis_cType”}
{$BusinesList=${$dis_cType[sn].2}}
{section name=sn2 loop=$BusinesList}