smarty模版

用Smarty输出php数组并且使用section循环以表格的形式显示。(section中 name属性的意义为循环的名称, loop 的意义为决定循环次数的变量名称)
在templates目录下创建一个test.tpl的模版,内容:<table border="1">
<{section name=row loop=$array}>
<tr>
<{section name=col loop=$array[$row]}>
<td><{$array[row][col]}></td>
</tr>


</table>
在smarty目录下创建一个php文件。
<?php
$array=array(array(1,'张三',25),array(2,'李四',27),array(3,'王武',30));
include 'Smarty.class.php';
$smarty=new Smarty();
$smarty->template_dir='demo/templates';
$smarty->compile_dir='demo/templates_c';
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
$smarty->assign('array',$array);
$smarty->display('test.tpl');


?>
用foreach循环(foreach from属性的意义为待循环数组的名称 item的意义为当前处理元素的变量名称  key 的意思为当前处理元素的键名)的话
test.tpl中这样写

<{foreach from=$array item=row name=a}>
<tr>

<{foreach from=$row item=value name=a} >
<td><{$value}></td>
<{/foreach}>

</tr>
<{/foreach}>
php文件中
$array=array(array("id"=>"1","name"=>"张三","age"=>"27"),array("id"=>"2","name"=>"三","age"=>"27"),array("id"=>"3","name"=>"张","age"=>"27"))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值