Smarty {foreach}, {foreachelse}

Source: Internet
Author: User

{Foreach} and {foreachelse} are used to access the associated array like the sequential number array.

Attribute Name

From: The array for loop access
Item: variable name of the current element
Key: The variable name of the current key name.
Name: name of the foreach loop used to access the foreach attribute
{Foreach from = $ variable item = item key = key name = name}

{$ Variable | @ count} // gets the length of the loop array

{$ Smarty. foreach. name. index} // returns the subscript of the array element of the current loop, starting with 0.

{$ Smarty. foreach. name. iteration} // gets the number of cycles, starting with 1

{$ Smarty. foreach. name. first} // when it is true, mark the first execution of the loop

{$ Smarty. foreach. name. last} // when it is true, mark the last execution of the loop

{$ Smarty. foreach. name. show} // whether the current display is displayed

{$ Smarty. foreach. name. total} // number of cycles

{/Foreach}

Foreach source) (performance indicator, iteration, first, last, display, overall.
<? Php
$ Arr = array (1000,100 1, 1002 );
$ Smarty-> assign ('myarray', $ arr );
?>

Template to output $ myArray in an un-ordered list

<Ul>
{Foreach from = $ myArray item = foo}
<Li >{$ foo} </li>
{/Foreach}
</Ul>
 

The above example will output:

<Ul>
<Li> 1000 </li>
<Li> 1001 </li>
<Li> 1002 </li>
</Ul>
 
 
Example 7-6. Demonstrates the item and key attributes

<? Php
$ Arr = array (9 => 'tennis ', 3 => 'canonicalization', 8 => 'coding ');
$ Smarty-> assign ('myarray', $ arr );
?>

Template to output $ myArray as key/val pair, like PHP's foreach.

<Ul>
{Foreach from = $ myArray key = k item = v}
<Li >{$ k }:{$ v} </li>
{/Foreach}
</Ul>
 

The above example will output:

<Ul>
<Li> 9: Tennis </li>
<Li> 3: padding Ming </li>
<Li> 8: Coding </li>
</Ul>
 
 
Example 7-7. {foreach} with associative item attribute

<? Php
$ Items_list = array (23 => array ('no' => 2456, 'label' => 'salad '),
96 => array ('no' => 4889, 'label' => 'cream ')
);
$ Smarty-> assign ('items ', $ items_list );
?>

Template to output $ items with $ myId in the url

<Ul>
{Foreach from = $ items key = myId item = I}
<Li> <a href = "item. php? Id = {$ myId} ">{$ I. no }:{$ I. label} </li>
{/Foreach}
</Ul>
 

The above example will output:

<Ul>
<Li> <a href = "item. php? Id = 23 "> 2456: Salad </li>
<Li> <a href = "item. php? Id = 96 "& gt; 4889: Cream </li>
</Ul>
 
 
Example 7-8. {foreach} with nested item and key

Assign an array to Smarty, the key contains the key for each looped value.

<? Php
$ Smarty-> assign ('contacts', array (
Array ('phone' => '1 ',
'Fax' => '2 ',
'Cell '=> '3 '),
Array ('phone' => '2014-555 ',
'Fax' => '2017-555 ',
'Cell '=> '2017-760 ')
));
?>

The template to output $ contact.

{Foreach name = outer item = contact from = $ contacts}
<Hr/>
{Foreach key = key item = item from = $ contact}
{$ Key }:{ $ item} <br/>
{/Foreach}
{/Foreach}
 

The above example will output:

<Hr/>
Phone: 1 <br/>
Fax: 2 <br/>
Cell: 3 <br/>
<Hr/>
Phone: 555-4444 <br/>
Fax: 555-3333 <br/>
Cell: 760-1234 <br/>

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.