Home >Backend Development >PHP Tutorial >Detailed description of smarty foreach_PHP tutorial
Regarding the knowledge of smarty foreach, here is a detailed explanation of its function and usage.
smarty {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 trade-off only works with a single array. Each {foreach} tag must be paired with a closing tag {/foreach}.
smarty foreach has the following attributes:
属性名称 | Type类型 | Required必要 | Default默认值 | Description描述 |
---|---|---|---|---|
from | array数组 | Yes必要 | n/a | 循环访问的数组 |
item | string字符串 | Yes必要 | n/a | 当前元素的变量名 |
key | string字符串 | No可选 | n/a | 当前键名的变量名 |
name | string字符 | No可选 | n/a | 用于访问foreach属性的foreach循环的名称 |
foreach is another solution for processing loops besides section (choose different solutions according to different needs).
foreach is used to process simple arrays (the elements in the array have the same type). Its format is much simpler than section. The disadvantage is that it can only process simple arrays.
foreach must be used in pairs with /foreach, and the from and item attributes must be specified.
The name attribute can be specified arbitrarily (a combination of letters, numbers and underscores).
The name of the {foreach} loop can be any combination of letters, arrays, and underscores. Please refer to PHP variables.
{foreach} loops can be nested, and the names of nested {foreach} should be different from each other.
The from attribute is usually an array of values and is used to determine the number of loops of {foreach}.
When there is no value in the from variable, {foreachelse} will be executed.
The {foreach} loop also has variables with its own properties, which can be accessed through {$smarty.foreach.name.property}, where "name" is the name property.
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.
smarty {foreach} In addition to the above attributes that are parameters, there are several non-parametric attributes: index, iteration, first, last, show, total. The following explains them one by one:
Attribute name | Description | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
index | Used to access the index value of the current foreach, index always starts from 0 | ||||||||||||||
iteration |
|
||||||||||||||
first | When the current foreach loop is executed for the first time, first is set to true |
||||||||||||||
last | When the current foreach loop executes to the last pass, last is set to true |
||||||||||||||
show | show is a parameter of foreach. The value is Boolean true or false. If false is specified, the loop will not display. If the loop specifies foreachelse clause, whether this clause is displayed or not also depends on the value of show | ||||||||||||||
total |
total is used to display the number of loop executions and can be called during or after the loop execution |
Let’s look at a few examples to illustrate the usage of each attribute of smarty foreach:
(1) Demonstrate item and key attributes
$arr = array(9 => 'Tennis', 3 => 'Swimming', 8 => 'Coding');
$smarty->assign('myArray', $arr);
?>
Use template key name/key value pairs to output $myArray, similar to PHP's foreach.
(2) When the item attribute of foreach is an associative array
$items_list = array(
23 => array('no' => 2456, 'label' => 'Salad'),
96 => array('no' => 4889, 'label' => 'Cream')
);
$smarty->assign('items', $items_list);
?>
In the template, the url outputs $items
(3) foreach uses nested items and keys
Set an array to Smarty that includes the key for each loop value corresponding to each key name.
$smarty->assign('contacts', array(
array('phone' => '1',
'fax' => '2',
'cell' => '3'),
array('phone' => '555-4444',
'fax' => '555-3333',
'cell' => '760-1234')
));
?>
Template for outputting $contact.
{foreach name=outer item=contact from=$contacts}
{* Output header block every five lines *}
Title |
---|
{$i.label} |
(5) smart foreach iteration attribute usage
{* This example will output 0|1, 1|2, 2|3, ... etc. *}
{foreach from=$myArray item=i name=foo}
{$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration},
{/foreach}
(6) smart foreach first attribute usage
{* Show LATEST instead of id for first entry *}
{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if} | {$i.label} |
(7) smart foreach last attribute usage
{* Add a horizontal marker at the end of the list *})
{foreach from=$items key=part_id item=prod name=products}
{$prod}{if $smarty.foreach.products.last}
(8) smart foreach show attribute usage
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.
(9) smart foreach total attribute usage
{* Display the line number at the end position *}
{foreach from=$items key=part_id item=prod name=foo}
{$prod.name>
ok, that’s all about smarty foreach. For more knowledge about smarty, please refer to this site: smarty knowledge topic