Smarty配置文件的使用 php
Smarty經過配置文件定義變量:
(1)建立配置文件:
配置文件能夠任意命名,其存儲路徑由Smarty對象的$config_dir屬性指定。若是存在不僅一個區域內使用的變量值,能夠使用三引號(""")將其完整地封裝起來。在建立配置文件時,建議在程序運行前使用"#"加一些註釋信息,以便閱讀、更新。
聲明變量:在[]以內的聲明的變量爲局部變量,在則有[]以外聲明的變量的則爲全局變量.
(2)加載配置文件:使用Smarty內建函數config_load語法以下:
{config_load file="file_name" section="add_attribute" scope="" global=""}
section:附加屬性,當配置文件中包含多個部分時應用,指定具體從哪一部份中取得變量
scope:加載數據的做用域,取值必須爲local、parent、blobal.local說明該變量的做用域爲當前模板;parent說明該變量的做用域爲當前模板和當前模板的父模板;global說明該變量的做用域爲全部模板;最後一個參數global說明加載的變量是否全局可見,等同於scope=parent.
(3)引用配置文件中的變量
配置文件加載成功後,就能夠在模板中引用配置文件中聲明的變量。引用配置文件應用的是"#"或者Smarty的保留變量$smarty.config,如:{config_load file="colors.conf" section="Database" scope="parent" }
the config file syntax:
# global variables //全局變量
pageTitle = "Main Menu"
bodyBgColor = #000000
tableBgColor = #000000
rowBgColor = #00ff00 html
[Customer] //局部變量
pageTitle = "Customer Info"
[Login]
pageTitle = "Login"
focus = "username"
Intro = """This is a value that spans more //三個引號定義的是全局變量,在全部區域內均可使用
than one line. you must enclose
it in triple quotes.""" dom
# hidden section
[.Database]
host=my.domain.com
db=ADDRESSBOOK
user=php-user
pass=foobar 函數
the tpl file to load config file:
{config_load file="colors.conf" section="Database" scope="parent" } <!--引用配置文件-->
<html>
<head>
<title>aa{#title#}</title> <!--/使用變量-->
</head>
<body style="background:{#rowBgColor#}"><!--使用變量 -->
hoho!
</body>
</html> spa