Smarty Foreach instructions

  • 2020-03-31 20:34:06
  • OfStack

Foreach is another way to handle loops in addition to sections (which can be different depending on your needs). Foreach is used to handle simple arrays (the elements in the array are of the same type), and its format is much simpler than section, with the disadvantage that it can only handle simple arrays.
Foreach must be used in pairs with /foreach, and the from and item attributes must be specified.
Iterates the data from the array specified by the from property into the variable specified by the item property.
Reference foreach (array_expression as $key => $value)
The from < = > Array_expression; The item < = > $value; The key < = > $key.
The name attribute can be specified arbitrarily (a combination of letters, Numbers, and underscores).
Foreach can be nested, but the nested foreach name must be unique.
The from attribute (usually an array) determines the number of loops.
The foreachelse statement is executed when the from attribute has no value. (if the value specified by the from attribute is null, use a foreachelse statement to specify - otherwise - what)
The foreach loop has its own variable names, using the variable name can access the loop. Using method for {$smarty. Foreach. Foreachname. Varname}, including foreachname is specified in the foreach name attribute.

The foreach demo
{* this example outputs the values of all elements in the array $custid *}
 
{foreach from=$custid item=curr_id} 
id: {$curr_id}<br> 
{/foreach} 

Output results:
Id: 1000
Id: 1001
Id: 1002
Foreach key demo and nested demo
{*
The array is defined as follows:
 
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"), 
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"))); 
*} 
{*  Keys are subscripts of arrays. See an explanation of arrays  *} 
{foreach name=outer item=contact from=$contacts} 
{foreach key=key item=item from=$contact} 
{$key}: {$item}<br> 
{/foreach} 
{/foreach} 

Output results:
Phone: 1.
Fax: 2
Cell: 3
Phone: 555-4444
Fax: 555-3333
Cell: 760-1234

The index
Index contains the index of the current array, starting at "0"
Such as:
 
<table> 
{foreach from=$items key=myId item=i name=foo} 
{if $smarty.foreach.foo.index % 5 == 0} {* $smarty.foreach.foo.index  right  5  For more than  *} 
<tr><th>Title</th></tr> 
{/if} 
<tr><td>{$i.label}</td></tr> 
{/foreach} 
</table> 

The iteration
Iteration contains the number of iterations of the current loop, always starting with 1 and adding 1 for each iteration.
Such as:
 
{*  The output  0|1, 1|2, 2|3, ...  , etc.  *} 
{foreach from=$myArray item=i name=foo} 
{$smarty.foreach.foo.index}|{$smarty.foreach.foo.iteration}, 
{/foreach} 

The first
First is set to true the first time the current foreach loop executes.
Such as:
 
{*  When the loop is first executed  LATEST , o  Otherwise the display  id *} 
<table> 
{foreach from=$items key=myId item=i name=foo} 
<tr> 
<td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td> 
<td>{$i.label}</td> 
</tr> 
{/foreach} 
</table> 

The last
Last is set to true when the current foreach loop executes until the last time.
Such as:
 
{*  Add a horizontal line at the end of the list  *} 
{foreach from=$items key=part_id item=prod name=products} 
<a href="#{$part_id}">{$prod}</a>{if $smarty.foreach.products.last}<hr>{else},{/if} 
{foreachelse} 
... content ... 
{/foreach} 

The total
Total is used to show the number of times a loop has been executed and can be called in a loop or after a loop has been executed.
Such as:
 
{*  The number of rows is displayed at the end  *} 
{foreach from=$items key=part_id item=prod name=foo} 
{$prod.label}<hr/> 
{if $smarty.foreach.foo.last} 
<div id="total">{$smarty.foreach.foo.total} items</div> 
{/if} 
{foreachelse} 
... something else ... 
{/foreach} 

Related articles: