Smarty變量用法詳解
本文實(shí)例講述了Smarty變量用法。分享給大家供大家參考,具體如下:
1. 從PHP分配的變量
調(diào)用從PHP分配的變量需在前加"$"符號(hào).(譯注:同php一樣)
調(diào)用模板內(nèi)的assign函數(shù)分配的變量也是這樣.(譯注:也是用$加變量名來(lái)調(diào)用)
示例:
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.
<p>
Your last login was on {$lastLoginDate}.
輸出:
Hello Doug, glad to see you could make it. <p> Your last login was on January 11th, 2001.
2. 從配置文件讀取的變量
配置文件中的變量需要通過(guò)用兩個(gè)"#"或者是smarty的保留變量 $smarty.config.來(lái)調(diào)用(后面會(huì)講到)
第二種語(yǔ)法在變量作為屬性值并被引號(hào)括住的時(shí)候非常有用.
(譯注:舉個(gè)例子 {include file="#includefile#"} 這樣#includefile#將被當(dāng)作字符處理,而不表示配置文件變量,但可以這樣表示{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"}
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
index.tpl:
{config_load file="foo.conf"}
<html>
<title>{$smarty.config.pageTitle}</title>
<body bgcolor="{$smarty.config.bodyBgColor}">
<table border="{$smarty.config.tableBorderSize}"bgcolor="{$smarty.config.tableBgColor}">
<tr bgcolor="{$smarty.config.rowBgColor}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
上述兩種模板寫(xiě)法都輸出:
<html>
<title>This is mine</title>
<body bgcolor="#eeeeee">
<table border="3" bgcolor="#bbbbbb">
<tr bgcolor="#cccccc">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
配置文件的變量只有在它們被加載以后才能使用.
更多關(guān)于Smarty相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《smarty模板入門(mén)基礎(chǔ)教程》、《PHP模板技術(shù)總結(jié)》、《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于smarty模板的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php set_include_path函數(shù)設(shè)置 include_path 配置選項(xiàng)
這篇文章主要介紹了php set_include_path函數(shù)設(shè)置include_path 配置選項(xiàng)的相關(guān)資料,需要的朋友可以參考下2016-10-10
PHP通過(guò)加鎖實(shí)現(xiàn)并發(fā)情況下?lián)尨a功能
本文基于php語(yǔ)言使用加鎖實(shí)現(xiàn)并發(fā)情況下?lián)尨a功能,特定時(shí)間段開(kāi)放搶碼并不允許開(kāi)放的碼重復(fù),本文介紹的非常詳細(xì),需要的朋友參考下2016-08-08
ThinkPHP中html:list標(biāo)簽用法分析
這篇文章主要介紹了ThinkPHP中html:list標(biāo)簽用法,較為詳細(xì)的分析總結(jié)了ThinkPHP中html:list標(biāo)簽的定義、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01
destoon會(huì)員注冊(cè)提示“數(shù)據(jù)校驗(yàn)失?。?)”解決方法
這篇文章主要介紹了destoon會(huì)員注冊(cè)提示“數(shù)據(jù)校驗(yàn)失?。?)”解決方法,需要的朋友可以參考下2014-06-06

