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 测试 {foreach $test as $test1} #使用as 关键字,在3的版本是支持的 {$test1.title} {$test1.author} {$test1.content} {/foreach} #foreach 结束[object Object]{foreach item=test1 from=$test} #别名 item 和 from 的使用 {$test1.title} {$test1.author} {$test1.content} {/foreach} #foreach 结束[object Object] include 文件 引入 #在include文件中定义一个{$title}变量 {include file='include.tpl' title='test include!'}[object Object] section 循环 {section name=test1 loop=$test} {$test[test1].title} {$test[test1].author} {$test[test1].content} {/section}[object Object] class && obj 的使用 {$obj->func(array('1','11'))}[object Object] function 函数的使用 {test1 p1='zeopean' p2='周'}
本文来自:Linux学习网数组