<?php #tpl.php 文件
#smarty 测试
#引入smarty
require_once "./libs/smarty/Smarty.class.php";
header("content-type:text/html;charset=utf-8");
#生成一个smarty对象
$smarty = new Smarty();
#设置smarty
$smarty->left_delimiter="{"; #左修饰符
$smarty->right_delimiter="}"; #右修饰符
$tpl=$smarty->template_dir ="./template"; #模板文件
$smarty->cache_dir ="./cache"; #缓存文件
$smarty->compile_dir ="./template_c"; #编译文件
$smarty->caching = true; #开启缓存
$smarty->cache_lifetime = 100; #生命周期
#smarty 数组遍历
$test = array(
array(
"title"=>"中文",
"author"=>"周",
"content"=>"你好 smarty"
),
array(
"title"=>"english",
"author"=>"zeopean",
"content"=>"hello Samrty"
)
);
#定义元素
$smarty->assign("test",$test);
#smarty 类与对象的使用
class Obj{
function func($param){
return $param[0].'====='.$param[1];
}
}
#实例化对象
$obj = new Obj();
$smarty->assign('obj',$obj);
#Smarty 函数
function test($param){
$p1= $param['p1'];
$p2= $param['p2'];
return $p1.'======='.$p2;
}
#注册函数
$smarty->registerPlugin('function','test1','test');
#输出
$smarty->display("index.tpl");
?>
index.tpl 模板文件
foreach 测试<br/>
{foreach $test as $test1} #使用as 关键字,在3的版本是支持的
{$test1.title}
{$test1.author}
{$test1.content}
{/foreach} #foreach 结束
<hr/>
{foreach item=test1 from=$test} #别名 item 和 from 的使用
{$test1.title}
{$test1.author}
{$test1.content}
<br/>
{/foreach} #foreach 结束
<hr/>
include 文件 引入<br/> #在include文件中定义一个{$title}变量
{include file='include.tpl' title='test include!'}
<hr/>
section 循环<br/>
{section name=test1 loop=$test}
{$test[test1].title}
{$test[test1].author}
{$test[test1].content}
<br/>
{/section}
<hr/>
class && obj 的使用
{$obj->func(array('1','11'))}
<hr/>
function 函数的使用
{test1 p1='zeopean' p2='周'}