Usage of smarty foreach

Source: Internet
Author: User

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.
Process Data traversal in the array specified by the from attribute to the variable specified by the item attribute.
See foreach (array_expression as $ key => $ value)
From <=> array_expression; item <=> $ value; key <=> $ key.
The name attribute can be arbitrarily specified (a combination of letters, numbers, and underscores ).
Foreach can be nested, but it must be unique in the nested foreach name.
The from attribute (usually an array) determines the number of cycles.
The foreachelse statement is executed when the from attribute has no value. (When the value specified by the from attribute is null, you can use the foreachelse statement to specify -- otherwise-what to do)
The foreach loop has its own variable name, which can be used to access the loop. usage: {$ smarty. foreach. foreachname. varname}, where foreachname is the name attribute specified in foreach.

Foreach demo
{* This example outputs the values of all elements in the array $ custid *}CopyCodeCode: {foreach from = $ custid item = curr_id}
ID: {$ curr_id} <br>
{/Foreach}

Output result:
ID: 1000
ID: 1001
ID: 1002
Foreach key demonstration and nested demonstration
{*
The array is defined as follows:Copy codeThe Code is as follows: $ smarty-> assign ("contacts", array (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. Please refer to the interpretation of the array *}
{Foreach name = outer item = contact from = $ contacts}
{Foreach key = key item = item from = $ contact}
{$ Key }:{$ item} <br>
{/Foreach}
{/Foreach}

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

. Index
Index contains the current array index, starting from "0"
For example:Copy codeThe Code is as follows: <Table>
{Foreach from = $ items key = myid item = I name = Foo}
{If $ smarty. foreach. Foo. Index % 5 = 0} {* $ smarty. foreach. Foo. Index evaluate the remainder of 5 *}
<Tr> <TH> title </Th> </tr>
{/If}
<Tr> <TD >{$ I. Label} </TD> </tr>
{/Foreach}
</Table>

. Iteration
Iteration includes the number of executions of the current loop, always starting from 1. Each execution is self-incremented by 1.
For example:Copy codeCode: {* output 0 | 1, 1 | 2, 2 | 3,..., etc *}
{Foreach from = $ myarray item = I name = Foo}
{$ Smarty. foreach. Foo. Index} | {$ smarty. foreach. Foo. Iteration },
{/Foreach}

. First
When the current foreach loop is executed for the first time, first is set to true.
For example:Copy codeThe Code is as follows: {* Latest is displayed when the loop is executed for the first time, or ID is displayed for o otherwise *}
<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
When the current foreach loop is executed until the last time, the last value is set to true.
For example:Copy codeThe Code is as follows: {* 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}

. total
total is used to display the number of times of cyclic execution. It can be called in a loop or after cyclic execution.
example: copy Code the code is as follows: {* number of lines displayed at the end *}
{foreach from = $ items key = part_id item = prod name = Foo}
{$ prod. label}


{if $ smarty. foreach. foo. last}
{$ smarty. foreach. foo. total} items

{/ 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.