欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Smarty變量用法詳解

 更新時(shí)間:2016年05月11日 10:01:25   作者:dingyan954  
這篇文章主要介紹了Smarty變量用法,結(jié)合實(shí)例形式分析了php分配變量與配置文件讀取變量的具體使用方法,需要的朋友可以參考下

本文實(shí)例講述了Smarty變量用法。分享給大家供大家參考,具體如下:

1. 從PHP分配的變量

調(diào)用從PHP分配的變量需在前加"$"符號(hào).(譯注:同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. 從配置文件讀取的變量

配置文件中的變量需要通過用兩個(gè)"#"或者是smarty的保留變量 $smarty.config.來調(diào)用(后面會(huì)講到)

第二種語法在變量作為屬性值并被引號(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>

上述兩種模板寫法都輸出:

<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運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家基于smarty模板的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論