Smartyforeach. Smartyforeach provides a detailed description of smartyforeach. Smarty {foreach} is used to access a digital index array like a loop.
The role and usage of smarty foreach are described in detail here.
Smarty {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.
Smarty foreach has the following attributes:
| Attribute name |
Type |
Required |
Default |
Description |
| From |
Array |
Yes required |
N/ |
Array of circular access
|
| Item |
String |
Yes required |
N/ |
Variable name of the current element
|
| Key |
String |
No |
N/ |
Variable name of the current key name
|
| Name |
String character |
No |
N/ |
Name of the foreach loop used to access the foreach attribute
|
Foreach is another solution for processing loops except section (different solutions are selected based on different needs ).
Foreach is used to process a simple array (the element types in the array are the same). Its format is much simpler than that of section. The disadvantage is that it can only process a simple array.
Foreach must be used in pairs with/foreach, and the from and item attributes must be specified.
The name attribute can be arbitrarily specified (a combination of letters, numbers, and underscores ).
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.
In addition to the preceding parameter attributes, smarty {foreach} has several non-parameter attributes: index, iteration, first, last, show, and total. The following describes them one by one:
| Attribute name |
Description |
| Index |
Used to access the current foreach index value. The index always starts from 0. |
| Iteration |
Iteration is used to display the number of executions of the current loop. iteration always starts from 1 and increases by 1 for each execution. |
| First |
When the current foreach loop is executed for the first time, the first parameter is set to true.
|
| Last |
When the current foreach loop is executed until the last timeLastSet to true
|
| Show |
ShowIs a parameter of foreach. The value is a Boolean value of true or false. if it is set to false, the loop is not displayed. if the foreachelse clause is specified in the loop, whether or not the clause is displayed depends onShowValue |
| Total |
TotalUsed to display the number of times of cyclic execution. it can be called in a loop or after cyclic execution.
|
The following examples illustrate the usage of the attributes of smarty foreach:
(1) demonstrate item and key attributes
$ Arr = array (9 => 'tennis ', 3 => 'canonicalization', 8 => 'coding ');
$ Smarty-> assign ('myarray', $ arr );
?>
Output $ myArray in the form of template key name/key-value pair, similar to PHP foreach.
{Foreach from = $ myArray key = k item = v}
- {$ K }:{ $ v}
{/Foreach}
The above example will output:
- 9: Tennis
- 3: Deming
- 8: Coding
(2) when the item attribute of foreach is associated with an 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 through $ myId
{Foreach from = $ items key = myId item = I}
- {$ I. no }:{$ I. label}
{/Foreach}
The above example will output:
(3) foreach uses nested items and keys
Set an array to Smarty. each cyclic value corresponding to each key name includes a key.
$ Smarty-> assign ('contacts', array (
Array ('phone' => '1 ',
'Fax' => '2 ',
'Cell '=> '3 '),
Array ('phone' => '2014-555 ',
'Fax' => '2017-555 ',
'Cell '=> '2017-760 ')
));
?>
The template used to output $ contact.
{Foreach name = outer item = contact from = $ contacts}
{Foreach key = key item = item from = $ contact}
{$ Key }:{ $ item}
{/Foreach}
{/Foreach}
The above example will output:
Phone: 1
Fax: 2
Cell: 3
Phone: 555-4444
Fax: 555-3333
Cell: 760-1234
(4) usage of the smarty foreach index attribute
{* Output the header block once per five lines *}
{Foreach from = $ items key = myId item = I name = foo}{If $ smarty. foreach. foo. index % 5 = 0}
| Title |
{/If}
| {$ I. label} |
{/Foreach}
(5) usage of the smarty foreach iteration attribute
{* This example outputs 0 | 1, 1 | 2, 2 | 3,... and so on *}
{Foreach from = $ myArray item = I name = foo}
{$ Smarty. foreach. foo. index} | {$ smarty. foreach. foo. iteration },
{/Foreach}
(6) usage of the smarty foreach first attribute
{* Display LATEST instead of id for the first entry *}
{Foreach from = $ items key = myId item = I name = foo}
| {If $ smarty. foreach. foo. first} LATEST {else} {$ myId} {/if} |
{$ I. label} |
{/Foreach}
(7) usage of the smarty foreach last attribute
{* Add a horizontal flag at the end of the list *})
{Foreach from = $ items key = part_id item = prod name = products}
{$ Prod} {if $ smarty. foreach. products. last} {else}, {/if}
{Foreachelse}
... Content...
{/Foreach}
(8) usage of the smarty foreach show attribute
Show is the {foreach} parameter. show is a Boolean value. If the value is FALSE, {foreach} is not displayed. If there is a corresponding {foreachelse}, it will be displayed.
(9) usage of the smarty foreach total attribute
{* Display the number of rows at the end position *}
{Foreach from = $ items key = part_id item = prod name = foo}
{$ Prod. name>
{If $ smarty. foreach. foo. last}
{$ Smarty. foreach. foo. total} items
{/If}
{Foreachelse}
... Something else...
{/Foreach}
Okay, there are so many summary about smarty foreach,