配置全域變數(shù)與段落變數(shù)
1,從設(shè)定檔讀取普通變數(shù)
#在configs資料夾下新建Smarty.conf檔
裡面寫(xiě)上變數(shù):
pageTitle = "This is mine" bodyBgColor = "#eeeeee"
引入範(fàn)本檔案:
##運(yùn)行結(jié)果如下:
#2,段落變數(shù)的使用:
#Smarty.conf檔:
#注釋 pageTitle = "This is mine" bodyBgColor = "#eeeeee" tableBorderSize = "3" tableBgColor = "#bbbbbb" rowBgColor = "#cccccc" #段落變量 [firstStyle] color='#00f' width='200px' height='300px' [.secondStyle] color='#eee' width='400px' height='500px' [other] other='這是其他'
test.html:
{* 加上section *} {config_load file='./configs/Smarty.conf' section='firstStyle'} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{#pageTitle#}</title> </head> <body> <!--html的注釋--> {*smarty模板的注釋*} 我叫{$address->name}今年{$address->age}歲<br> 性別:{$smarty.const.CL}<br> 配置變量1:{#color#}<br> 配置變量2:{$smarty.config.width}<br> 配置變量3:{$smarty.config.height}<br> 配置變量4:{$smarty.config.other}<br> </body> </html>
執(zhí)行結(jié)果:
注意:
?????
??1、若全域變數(shù)與被載入的段落變數(shù)有相同的變數(shù)名,則段落名的值將覆蓋全域變數(shù)的值。
2、若某個(gè)段落變數(shù)裡含有相同的變數(shù)名,則最後一個(gè)的變數(shù)的值將會(huì)覆寫(xiě)前面的值。
3、在整個(gè)smarty.conf檔案中,點(diǎn)(.)擁有相對(duì)較高的權(quán)限的。點(diǎn)的作用是將一個(gè)變數(shù)或整個(gè)段落隱藏,不能被使用。
3,段落變數(shù)的簡(jiǎn)單應(yīng)用:(方便更換div的樣式)
#test.html:
{config_load file='./configs/Smarty.conf' section='firstStyle'} <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>smarty test1</title> <style type="text/css"> #aa{ width: {#width#};height: {#height#};background: {#color#};} </style> </head> <body> <div id='aa'> 這是一個(gè)div<br/><br/> {#content#} </div> </body>
要注意的是:{}號(hào)在smarty模板中預(yù)設(shè)都會(huì)被解析
Smarty.conf:#
#段落變量 #第一種顏色風(fēng)格 [firstStyle] color='#00f' width='300px' height='300px' content='第一種風(fēng)格' #第二種顏色風(fēng)格 [secondStyle] color='#0f0' width='500px' height='500px' content='第二種風(fēng)格'
firstStyle展示:
secondStyle展示: