一、安裝
注:這里所使用的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);//索引數組1 7 $smarty->assign('arr2', $arr2);//關聯數組2 8 $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`"}>
但並不推薦使用這種方式,應當將更多的邏輯判斷放到邏輯業務層中去處理。
