配置全局變量與段落變量
1,從配置文件中讀取普通變量
在configs文件夾下新建Smarty.conf文件
里面寫上變量:
pageTitle = "This is mine" bodyBgColor = "#eeeeee"
引入模板文件:
運行結(jié)果如下:
2,段落變量的使用:
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>
運行結(jié)果:
注意:
???????1、若全局變量與被加載的段落變量有相同的變量名,則段落名的值將覆蓋全局變量的值。
2、若某個段落變量里含有相同的變量名,則最后一個的變量的值將會覆蓋前面的值。
3、在整個smarty.conf文件中,點(.)擁有相對較高的權(quán)限的。點的作用是將一個變量或者整個段落隱藏,不能被使用。
3,段落變量的簡單應(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'> 這是一個div<br/><br/> {#content#} </div> </body>
需要注意的是:{}號在smarty模板中默認(rèn)都會被解析
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展示: