smarty中的assign()函数和display()函数section()实现循环功能

smarty中的assign()函数和display()函数section()实现循环功能

一:以下是smarty配制文件smarty_inc.php
<?php
include_once("Smarty/Smarty.class.php");//包含smarty类文件
$smarty = new smarty();//建立Smarty实例对象$Smarty
$smarty->config_dir="Smarty/Config_File.class.php";//目录变量
$smarty->caching=false;//是否使用缓存,项目在调试期间,不建议使用缓存
$smarty->template_dir = "./templates";//设置模板目录
$smarty->compile_dir = "./templates_c";//设置编译目录
$smarty->cache_dir="./smarty_cache";//缓存文件夹
//------------------------
//左右边界符,默认为{},但实际应用当中容易与Javascript冲突
//-------------------------------
$smarty->left_delimiter="{";
$smarty->right_delimiter= "}";
?>

二:以下的是文件index.php
<?php
/*
$smarty->assign("name", "肖红阳"):
该数的原型为assign(string varname, mixed var),varname为模板中使用的模板变量,var指出要将模板变量替换的变量名;其第二种原形为assign(mixed var),我们要在后面的例子详细的讲解这个成员函数的使用方法,assign是Smarty的核心函数之一,所有对模板变量的替换都要使用它。
$smarty->display("index.tpl"):
该函数原形为display(string varname),作用为显示一个模板。简单的讲,它将分析处理过的模板显示出来,这里的模板文件不用加路径,只要使用一个文件名就可以了,它路径我们已经在$smarty->templates(string path)中定义过了

*/

include("smarty_inc.php");                                          //引用smarty配制文件
$array[]=array("note"=>"坚持__不懈","date"=>"2010-07-01");    //定义数组
$array[]=array("note"=>"成功__之路","date"=>"2010-07-02");
$array[]=array("note"=>"程序__之道","date"=>"2010-07-03");
$array[]=array("note"=>"PHP___学习","date"=>"2010-07-04");

$col1="标题";
$col2="日期";
$smarty->assign("col1",$col1);           //进行模板变量替换
$smarty->assign("col2",$col2);
$smarty->assign("title",$array);
$smarty->display("index.htm");           //将分析处理过的模板显示出来

?>


三:以下是文件./templates/index.htm
<html>
<head>
<title>
</title>
</head>
<body>
<!--
section name = name loop = $varName[, start = $start, step = $step, max = $max, show = true]
name是section的名称,不用加$
$loop: 要循环的变量,在程序中要使用assign对这个变量进行操作。
$start: 开始循环的下标,循环下标默认由0开始
$step: 每次循环时下标的增数
$max: 最大循环下标
$show: boolean类型,决定是否对这个块进行显示,默认为true
-->
<h2>{$col1}&nbsp;&nbsp;&nbsp; {$col2}</h2>
{section name=list loop=$title}
{$title[list].note}--{$title[list].date}
<br>
{sectionelse}
<tr><td colspan="5">No items found</td></tr>         //如果数组不存在则输出提示No items found
{/section}
</body>
</html>

index.php文件运行结果如下:

标题    日期

坚持__不懈--2010-07-01 
成功__之路--2010-07-02 
程序__之道--2010-07-03 
PHP___学习--2010-07-04

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值