<?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='周'}
本文來自:Linux學習網php