Smarty Foreach Use description _php template

Source: Internet
Author: User
foreach is another option for dealing with loops in addition to section (choose different scenarios depending on the needs). foreach is used to work with simple arrays (the types of elements in an array are consistent), its format is much simpler than the section, and the disadvantage is that only simple arrays can be processed.
foreach must be used in pairs with/foreach, and the From and item properties must be specified.
Iterates through the data in the array specified by the From property to the variable specified by the Item property.
Reference foreach (array_expression as $key => $value)
From <=> array_expression;item <=> $value key <=> $key.
The Name property can be arbitrarily specified (a combination of letters, numbers, and underscores).
foreach can be nested, but it must be guaranteed that the foreach name in the nesting is unique.
The From property (usually an array) determines the number of loops.
The Foreachelse statement is executed when the From property has no value. (The From property specifies a null value, you can specify with the Foreachelse statement--otherwise--what?)
The Foreach loop has its own variable name that you can use to access the loop. Use the method {$smarty. Foreach.foreachname.varname}, where Foreachname is the Name property specified in foreach.

foreach Demo
{* This example will output the value of all elements in the array $custid *}
Copy Code code as follows:

{foreach from= $custid item=curr_id}
ID: {$curr _id}<br>
{/foreach}

Output results:
id:1000
id:1001
id:1002
Demo and nested demo of the foreach Key
{*
The array is defined as follows:
Copy Code code as follows:

$smarty->assign ("Contacts", Array ("Phone" => "1", "Fax" => "2", "Cell" => "3"),
Array ("Phone" => "555-4444", "Fax" => "555-3333", "Cell" => "760-1234"));
*}
The {* Key is the subscript of the array, see interpretation of the array *}
{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

. Index
Index contains current array indices, starting with "0"
For example:
Copy Code code as follows:

<table>
{foreach from= $items key=myid item=i Name=foo}
{if $smarty. Foreach.foo.index% 5 = 0} {* $smarty. Foreach.foo.index to 5 *}
<tr><th>Title</th></tr>
{/if}
<tr><td>{$i .label}</td></tr>
{/foreach}
</table>

. iteration
Iteration contains the number of executions of the current loop, always starting at 1, and adding 1 each time.
For example:
Copy Code code as follows:

{* Output 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}

. the
The first time the current Foreach loop executes is set to true.
For example:
Copy Code code as follows:

{* Show LATEST when the loop is first executed, o otherwise 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>

. Last
Last is set to true at the end of the current foreach loop execution.
For example:
Copy Code code as follows:

{* Add horizontal line at the end of List *}
{foreach from= $items key=part_id item=prod name=products}
<a href= "#{$part _id}" >{$prod}</a>{if $smarty. foreach.products.last}{Foreachelse}
... content ...
{/foreach}

. Total
Total is used to show the number of times the loop executes, which can be invoked either in a loop or after a loop execution.
For example:
Copy Code code as follows:

{* Displays the number of rows at the end *}
{foreach from= $items key=part_id item=prod Name=foo}
{$prod .label}{if $smarty. Foreach.foo.last}
<div id= "Total" >{$smarty. Foreach.foo.total} items</div>
{/if}
{Foreachelse}
... something else ...
{/foreach}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.