PHP Smarty引擎 foreach循环语法

模板:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
{assign var="title" value="网页标题"}
<title>{$title}</title>
</head>
<body>
{foreach from=$array item=arrayitem}
{$arrayitem}<br />
{/foreach}
</body>
</html>

带key的数组,可以用foreach的key属性指定,如:

{foreach from=$array item=arrayitem key=index}
{$index}:{$arrayitem}
{/foreach}

php文件:


<?php
require_once 'libs/Smarty.class.php';
$smarty=new Smarty();
$array=array("a","b","c","d");
$smarty->assign("array",$array);
$smarty->display("index.html");
?>

© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记