The foreach traversal array instance of Smarty template technology is fully explained

Source: Internet
Author: User
Tags php foreach smarty template

first, the Item property usage<?Php$arr= Array ( -,851,7412); $smarty->assign ('Testarrg', $arr);?>use the Foreach method in Smarty to traverse and output this array<dl><dt>foreach the Item property usage </dt>{foreach  from= $testarrg item=Test}<dd>{$test}</dd>{/foreach}</dl>through the above will output:<dl><dt>foreach the Item property usage </dt><dl> -</dl><dl>851</dl><dl>7412</dl></dl>Ii. A demonstration of the item and key properties<?Php$arr= Array (1='test1',2='test2',3='test3'); $smarty->assign ('Testarray', $arr);?>The above example is followed by an array of keys/template key name for value pair form/the form of a key-value pair is output $testarray, similar to the PHP foreach. Item and key attribute usage in <dl><dt>foreach </dt>{foreach  from= $testarray Key=key item=Value}<dd>{$key}: {$value}</dd>{/foreach}</dl>The above example will output:Item and key attribute usage in <dl><dt>foreach </dt><dd>1: test1</dd><dd>2: test1</dd><dd>3: test2</dd></dl>third, about {foreachthe Item property in the} is a demonstration case of an associative array<?php$test_list=array (Ten=>array ('a'=1256,'b'='Testok'), One=>array ('a'=8959,'b'='oktest') ); $smarty->assign ('Arr_item', $test _list);?>method of writing a Foreach loop in a TPL template<ul>{foreach  from= $test _list key="Key"item="Val"}<li>{$val. A}: {$val .b}</li>{/foreach}</ul>The example above will output:<ul><li>1256: testok</li><li>8959: oktest</li></ul>Four, {foreach} using nested item and key Assign an array to Smarty, the key contains the key foreach looped value. Sets an array to smarty, including the key for Each loop value corresponding to each key name. <?Php$_arrg=array (Array ('Photo'= -,'Phfax'= $,'Phcell'= -), Array ('Phone'='Hello','Fax'='Word','Cell'='Hess') ); $smarty->assign ('ARRG', $_ARRG);?>use the foreach output template to export {foreachName="Outgow"item="CT"  from=$ARRG}{foreachkey="Keys"item="IMS"  from=$ARRG} {$keys}: {$ims}<br/>{/foreach}{/foreachThe example above will output:Photo: -<br/>Phfax: $<br/>Phcell: -<br/>Phone:hello<br/>Fax:word<br/>cell:hess<br/>v. The use of the property index for foreach {* Three lines to output the head once *}<table width=" -"Boder="1">{foreach  from= $items Key=myid item=" is"Name="fot"}{if$smarty.foreach. Fot.index%3==0}//In addition to more than 3 of 0<tr><th> article title </th></tr>{/if}<tr><td>{$ is.label}</td></tr>{/foreach}</table>The usage of the attribute iteration about foreach iteration shows the current number of cycles, which, unlike the Index property, is 1 each time the loop grows by 1. www.it165.net{foreach  from= $myArray Item=i name=Foo} {$smarty.foreach. foo.index}| {$smarty.foreach. foo.iteration},{/foreach} vii. usage of the property first of the Foreach property returns true {* For the first entry display latest instead of 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>viii. usage of the last attribute of the Foreach Property Lase property returns True {* Add a horizontal marker 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}Else},{/if}{foreachelse} ... content ... {/foreach} ix. the use of the properties of the Foreach show the Show property returns a Boolean value that, when it is true (ture), shows that the loop is not false (false), the loop is not displayed.

The foreach traversal array instance of Smarty template technology is fully explained

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.