smarty 中foreach中iteration变量值就是当前循环次数,在显示序号递增时候用到

http://www.justwinit.cn/post/1594/


smarty 中foreach中iteration变量值就是当前循环次数
例子:

<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");
?>

main.php


<?php
include   "../drivers/smarty/Smarty.class.php";
$tpl= new Smarty();
$tpl->left_delimiter = "<{";
$tpl->right_delimiter = "}>";

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值