Smarty變量用法詳解
本文實例講述了Smarty變量用法。分享給大家供大家參考,具體如下:
1. 從PHP分配的變量
調(diào)用從PHP分配的變量需在前加"$"符號.(譯注:同php一樣)
調(diào)用模板內(nèi)的assign函數(shù)分配的變量也是這樣.(譯注:也是用$加變量名來調(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. 從配置文件讀取的變量
配置文件中的變量需要通過用兩個"#"或者是smarty的保留變量 $smarty.config.來調(diào)用(后面會講到)
第二種語法在變量作為屬性值并被引號括住的時候非常有用.
(譯注:舉個例子 {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>
上述兩種模板寫法都輸出:
<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)容感興趣的讀者可查看本站專題:《smarty模板入門基礎(chǔ)教程》、《PHP模板技術(shù)總結(jié)》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于smarty模板的PHP程序設(shè)計有所幫助。
相關(guān)文章
Bo-Blog專用的給Windows服務(wù)器的IIS Rewrite程序
Bo-Blog專用的給Windows服務(wù)器的IIS Rewrite程序...2007-08-08
ThinkPHP3.2.3框架郵件發(fā)送功能圖文實例詳解
這篇文章主要介紹了ThinkPHP3.2.3框架郵件發(fā)送功能,結(jié)合圖文與實例形式詳細(xì)分析了基于thinkPHP框架進(jìn)行郵件發(fā)送的相關(guān)原理、配置及操作技巧,需要的朋友可以參考下2019-04-04
laravel excel 上傳文件保存到本地服務(wù)器功能
今天小編就為大家分享一篇laravel excel 上傳文件保存到本地服務(wù)器功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

