.. It's cold!
{/if}
Note that enclosing the conditional statement within parentheses is optional,
although it??™s required in standard PHP code.
The foreach Function
The foreach function operates much like the namesake in the PHP language. As you??™ll
soon see, the syntax is quite different, however. Four parameters are available, two of
which are required:
from: This required parameter specifies the name of the target array.
item: This required parameter determines the name of the current element.
key: This optional parameter determines the name of the current key.
name: This optional parameter determines the name of the section. The name is
arbitrary and should be set to whatever you deem descriptive of the section??™s
purpose.
Consider an example. Suppose you want to loop through the days of the week:
$smarty = new Smarty;
$daysofweek = array("Mon.","Tues.","Weds.","Thurs.","Fri.","Sat.","Sun.");
$smarty->assign("daysofweek", $daysofweek);
$smarty->display("daysofweek.tpl");
The daysofweek.tpl template contains the following:
{foreach from=$daysofweek item=day}
{$day}
{/foreach}
486 CHAPTER 19 ?– T EMPLAT I NG WITH SMARTY
This returns the following:
Mon.
Tues.
Weds.
Thurs.
Fri.
Sat.
Sun.
You can use the key attribute to iterate through an associative array. Consider this
example:
$smarty = new Smarty;
$states = array("OH" => "Ohio", "CA" => "California", "NY" => "New York");
$smarty->assign("states",$states);
$smarty->display("states.
Pages:
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567