一、安裝
註:這裡所使用的Smarty是3.x版本,要求PHP版本為5.2或者更高。
解壓下載下來的Smarty壓縮檔,將檔案裡lib目錄下的所有檔案複製到項目中的指定目錄中,例如"/lib/Smarty/"目錄下,在項目中引入"Smarty.class.php"檔案後既可使用。
在Smarty內部會自動定義一個名為SMARTY_DIR的常量,該常量預設指向Smarty.class.php所在的絕對路徑,可以通過自行設定該值來改變預設值,如:define('SMARTY_DIR',
'/lib/smarty/')。註:SMARTY_DIR常量必須以'/'結尾。
1 //直接引入smarty檔案2 require_once 'lib/smarty/Smarty.class.php';3 //或通過定義SMARTY_DIR常量引入檔案4 define('SMARTY_DIR', 'lib/smarty/');5 require_once SMARTY_DIR . 'Smarty.class.php';6 7 //執行個體化一個Smarty對象8 $smarty = new Smarty();
在正式使用smarty之前,還需要設定幾個基本路徑:
- $template_dir 用於存放smarty模版檔案,如果未指定該路徑,Smarty會自動在目前的目錄中尋找模版檔案。
- $compile_dir 用於存放smarty編譯後的檔案,如果不指定該目錄,Smarty會自動在當前指令碼所在目錄下產生一個名為'templates_c'的目錄用於存放產生的編譯檔案。
- $config_dir 用於存放smarty設定檔
- $cache_dir 用於存放smarty快取檔案,如果開啟Smarty緩衝功能,但沒有指定該目錄,Smarty會自動在當前指令碼所在目錄下產生一個名為'cache'的目錄用於存放快取檔案。
1 $smarty->template_dir = 'views/';2 $smarty->compile_dir = 'lib/smarty/templates_c/';3 $smarty->config_dir = 'lib/smarty/configs/';4 $smarty->cache_dir = 'lib/smarty/cache/';5 //或者6 $smarty->setTemplateDir(''views/';');7 $smarty->setCompileDir('lib/smarty/templates_c/');8 $smarty->setConfigDir('lib/smarty/configs/');9 $smarty->setCacheDir('lib/smarty/cache/');
配置完成後,可通過Smarty的testInstall()內建方法查看配置資訊:
1 $smarty->testInstall();
二、使用Smarty
Smarty配置完成以後,既可通過Smarty調用指定模版,首先在模版檔案路徑下(這裡是'view/')建立一個模版檔案home.tpl,內容如下:
1 <html> 2 <head> 3 <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 4 <title>Home</title> 5 <link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" title="no title" charset="utf-8" /> 6 </head> 7 <body> 8 Smarty 9 </body>10 </html>
註:模版檔案可以是任意尾碼名,為了安全 起見,盡量避免使用.html、.php等可以直接被瀏覽器解析的檔案,當然最好的辦法是禁止web瀏覽器直接存取模版所在檔案夾。
模版的調用
通過Smarty的內建方法display()來開啟一個模版檔案
void display(string template, string cache_id, string compile_id);
對與上述方法中涉及到的參數,將會在後面介紹
1 $smarty->display(home.tpl);
變數的傳遞與訪問
同時Smarty也可以通過內建方法assign()向模版檔案中傳遞各種變數,如數值、字串、數組、對象等。
void assign(string varname, mixed var, bool nocache);
對與上述方法中涉及到的參數,將會在後面介紹
1 $arr1 = new Array('zh', '26');2 $arr2 = new Array('name'=>'zh', 'age'=>26);3 $obj = new MyObject();//MyObject為自訂類,包含name等公用屬性4 $smarty->assign('str', 'zh');//字串5 $smarty->assign('num', 6);//數值型6 $smarty->assign('arr1', $arr1);//索引數組17 $smarty->assign('arr2', $arr2);//關聯陣列28 $smarty->assign('obj', $obj);//對象
模版中使用變數:
在模版中調用變數,通過在變數之前加上$,並使用'{}'邊界符將變數括起來,如home.tpl內容如下:
1 <body>2 {$str} <!-- 輸出zh -->3 {$num} <!-- 輸出zh -->4 {$arr1[0]} <!-- 輸出zh -->5 {$arr2.name} <!-- 輸出zh -->6 {$obj->name} <!-- 輸出zh -->7 </body>
可以看出,Smarty訪問通過指定數組的索引來訪問索引數組,通過'.'訪問關聯陣列,通過'->'訪問對象屬性。
註:預設情況下,左邊界符'{'與變數之間不能存在空格,否則變數將不會被解析,而是連同'{' 和 '}'原樣顯示出來。可以通過設定$smarty->auto_literal = false;來禁止這種
行為。
'{}'是Smarty的預設邊界符,可以通過修改$left_delimiter和$right_delimiter屬性來修改預設邊界符,如:
1 $smarty->left_delimiter = '<{';2 $smarty->right_delimiter = '}>';
Smarty支援模版內部注釋,通過使用'{**}',如:{*這裡是注釋*}。Smarty中的注釋與HTML注釋<!-- -->有些不同,Smarty中的注釋,並不會顯示在網頁源檔案中。
注:如果修改了Smarty的預設邊界符,相應的注釋標籤也需要變動,如<{*注釋*}>,而且以下所有標籤邊界符均改為該種形式。
有時可能需要模版忽略變數的解析,而將原文顯示出來,如:要在頁面中顯示出<{$name}>,而不是將$name的變數值顯示出來,可以通過<{ldelim}> , <{rdelim}> 或 <{$smarty.ldelim}> , <{$smarty.rdelim}>來實現,如:
<{ldelim}>$name<{rdelim}><{$smarty.ldelim}>$name<{$smarty.ldelim}>
另一種方法是通過使用Smarty內建塊標籤<{literal}>,Smarty將會忽略解析出現在該標籤之間的所有內容,如:
1 <{literal}>2 這裡輸入不想被Smarty解析的內容3 <{/literal}>
引號
與PHP類似,Smarty會解析出現在雙引號中的變數,但該變數只能包含數字、字元和底線,如果包含其它字元,則必須使用"`"(ESC鍵)將變數包含起來
1 <{* $tpl_name將會別當作變數解析 *}> 2 <{include file="subdir/$tpl_name.tpl"}> 3 4 <{* 不會解析任何變數,只有雙引號括起來的變數才會別解析 *}> 5 <{include file='subdir/$tpl_name.tpl'}> 6 7 <{* $smarty.config.myva將會別解析 *}> 8 <{cycle values="one,two,`$smarty.config.myval`"}> 9 10 <{* $module.contact將會別解析 *}>11 <{include file="`$module.contact`.tpl"}>12 13 <{* $module.$view都將會被解析 *}>14 <{include file="`$module.$view`.tpl"}>
數學運算式
Smarty支援在模版中直接使用數學運算式,如:
1 <{$foo+1}> 2 3 <{$foo*$bar}> 4 5 <{* some more complicated examples *}> 6 7 <{$foo->bar-$bar[1]*$baz->foo->bar()-3*7}> 8 9 <{if ($foo+$bar.test%$baz*134232+10+$b+10)}>10 11 <{$foo|truncate:"`$fooTruncCount/$barTruncFactor-1`"}>12 13 <{assign var="foo" value="`$foo+$bar`"}>
但並不推薦使用這種方式,應當將更多的邏輯判斷放到邏輯業務層中去處理。