smarty模板主要提供了foreach和section两种方法来实现循环。
foreach的语法格式:
{foreach key=key1 item=item1 from=$array1}
{$item1}
{/foreach}
key1--------表示数组中的每一个键值
item1 ------------ 表示数组中的每一个元素
$array1-------表示传入的数组变量名称
------------------------------------------------------------------------------------------------------------------------------------------
eg:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
$array1 = array(1=>"simon",2=>"helen",3=>"petter",4=>"中国");
-------------------------------------------------------------------------------------------------------------------------------------------
{section name=section1 loop=$array1}
{$array1[section1]}
{/section}
section1--------表示这个循环区域的名字
$array1 ---------- 表示传入的数组变量名称。
需要注意:传递给section的数组的键值必须是以0开始的正整数,否则数组中的内容不会正确地被section获得
eg:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
$smarty->assign("array1",$array1);
-----------------------------------------------------------------------------------------------------------------------
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
$array1 = array(
$smarty->assign("array1",$array1);
?>