本文執行個體講述了Smarty變數用法。分享給大家供大家參考,具體如下:
1. 從PHP分配的變數
調用從PHP分配的變數需在前加"$"符號.(譯註:同php一樣)
調用模板內的assign函數分配的變數也是這樣.(譯註:也是用$加變數名來調用)
樣本:
index.php:
$smarty = new Smarty;$smarty->assign('firstname', 'Doug');$smarty->assign('lastLoginDate', 'January11th, 2001');$smarty->display('index.tpl');
index.tpl:
Hello {$firstname}, glad to see you couldmake it.Your last login was on {$lastLoginDate}.
輸出:
Hello Doug, glad to see you could make it.Your last login was on January 11th, 2001.
2. 從設定檔讀取的變數
設定檔中的變數需要通過用兩個"#"或者是smarty的保留變數 $smarty.config.來調用(後面會講到)
第二種文法在變數作為屬性值並被引號括住的時候非常有用.
(譯註:舉個例子 {include file="#includefile#"} 這樣#includefile#將被當作字元處理,而不表示設定檔變數,但可以這樣表示{include file="`$smarty.config.includefile`"}不要忘了加``)
樣本:
foo.conf:
pageTitle = "This is mine"bodyBgColor = "#eeeeee"tableBorderSize = "3"tableBgColor = "#bbbbbb"rowBgColor = "#cccccc"
index.tpl:
{config_load file="foo.conf"}{#pageTitle#}
index.tpl:
{config_load file="foo.conf"}{$smarty.config.pageTitle}
上述兩種模板寫法都輸出:
This is mine
設定檔的變數只有在它們被載入以後才能使用.
更多關於Smarty相關內容感興趣的讀者可查看本站專題:《smarty模板入門基礎教程》、《PHP模板技術總結》、《PHP基於pdo操作資料庫技巧總結》、《PHP運算與運算子用法總結》、《PHP網路編程技巧總結》、《PHP基本文法入門教程》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家基於smarty模板的PHP程式設計有所協助。