当前位置:多学网学习教育电脑学习编程入门PHP教程smarty中的assign()函数和display()函数section()实现

smarty中的assign()函数和display()函数section()实现

[08-23 22:08:45]   来源:http://www.duoxue8.com  PHP教程   阅读:712
smarty中的assign()函数和display()函数section()实现,标签:PHP技巧,php培训,php学习,php安装,http://www.duoxue8.com

一:以下是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}    {$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




smarty中的assign()函数和display()函数section()实现 结束。
Tag:PHP教程PHP技巧,php培训,php学习,php安装电脑学习 - 编程入门 - PHP教程