smarty 中foreach中iteration变量值就是当前循环次数
例子:
注意:得加上:name=name {$smarty.foreach.name.iteration} 依次循环会显示:1,2,3,4...
详细的例子如下:
HTML:
<html>
<head>
<title><{$title}></title>
</head>
<body>
<{$content}>
<{foreach from=$array item=foreach name=name}>
<{$foreach.newsID}><br>
<{$foreach.newsTitle}>
<h1>
<{$smarty.foreach.name.iteration}></h1><br>
<{/foreach}>
</body>
</html>
PHP:
main.php
例子:
<code>
{foreach key=key item=item from=$contact name=name}
{$key}: {$item}:{$smarty.foreach.name.iteration}<br>
{/foreach>
</code>
{foreach key=key item=item from=$contact name=name}
{$key}: {$item}:{$smarty.foreach.name.iteration}<br>
{/foreach>
</code>
注意:得加上:name=name {$smarty.foreach.name.iteration} 依次循环会显示:1,2,3,4...
详细的例子如下:
HTML:
<html>
<head>
<title><{$title}></title>
</head>
<body>
<{$content}>
<{foreach from=$array item=foreach name=name}>
<{$foreach.newsID}><br>
<{$foreach.newsTitle}>
<h1>
<{$smarty.foreach.name.iteration}></h1><br>
<{/foreach}>
</body>
</html>
PHP:
<?php
require "main.php";
$tpl->template_dir = "./aaa";
$array[] = array("newsID"=>1, "newsTitle"=>"第1条新闻");
$array[] = array("newsID"=>2, "newsTitle"=>"第2条新闻");
$array[] = array("newsID"=>3, "newsTitle"=>"第3条新闻");
$array[] = array("newsID"=>4, "newsTitle"=>"第4条新闻");
$array[] = array("newsID"=>5, "newsTitle"=>"第5条新闻");
$array[] = array("newsID"=>6, "newsTitle"=>"第6条新闻");
$tpl -> assign("array",$array);
$tpl -> assign("title","测试标题");
$tpl -> assign("content","Hello,World");
$tpl -> display("test.htm");
?>
require "main.php";
$tpl->template_dir = "./aaa";
$array[] = array("newsID"=>1, "newsTitle"=>"第1条新闻");
$array[] = array("newsID"=>2, "newsTitle"=>"第2条新闻");
$array[] = array("newsID"=>3, "newsTitle"=>"第3条新闻");
$array[] = array("newsID"=>4, "newsTitle"=>"第4条新闻");
$array[] = array("newsID"=>5, "newsTitle"=>"第5条新闻");
$array[] = array("newsID"=>6, "newsTitle"=>"第6条新闻");
$tpl -> assign("array",$array);
$tpl -> assign("title","测试标题");
$tpl -> assign("content","Hello,World");
$tpl -> display("test.htm");
?>
main.php
<?php
include "../drivers/smarty/Smarty.class.php";
$tpl= new Smarty();
$tpl->left_delimiter = "<{";
$tpl->right_delimiter = "}>";
?>
include "../drivers/smarty/Smarty.class.php";
$tpl= new Smarty();
$tpl->left_delimiter = "<{";
$tpl->right_delimiter = "}>";
?>
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1594/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2009-4-9 10:28
评论列表
2010-7-15 21:28 | p90x
vb
分页: 1/1 1