Smarty 學習筆記四 配置文件的讀取


設置配置文件
Smarty配置文件用於存放全局變量,例如將模版顏色存放到配置文件中,這樣,當修改模版顏色時,不需逐個的去修改每個模版,而是直接修改配置文件既可。配置文件格式如下:

 1 # global variables
 2 pageTitle = "Main Menu"
 3 bodyBgColor = #000000
 4 tableBgColor = #000000
 5 rowBgColor = #00ff00
 6 
 7 [Customer]
 8 pageTitle = "Customer Info"
 9 
10 [Login]
11 pageTitle = "Login"
12 focus = "username"
13 Intro = """This is a value that spans more
14 than one line. you must enclose
15  it in triple quotes."""
16 
17 # hidden section
18  [.Database]
19 host=my.example.com
20 db=ADDRESSBOOK
21 user=php-user
22 pass=foobar

 

配置文件支持注釋,只需在要注釋的行開始處使用#既可。
也可將配置文件分為多個塊,在不同的模版中使用不同的配置文件塊區域。塊名由"[]"括起來,名稱可以是任意不包含"[" 和 "]"的字符串。如上例中的[Customer]和[Login]。
上例配置文件中,前4個變量不屬於任何塊,讀取配置文件時,它們將總被獲取到,即使是讀取指定塊區域的配置文件。如果塊區域中的變量名與全局變量名重名,並且$config_overwrite為true時,塊區域變量值將會替換全局變量的值。
配置文件路徑
在使用配置文件之前,需要先指定配置文件所在路徑,可通過下列方法實現
$config_dir
用來定義存放配置文件的路徑信息,默認路徑是./configs,即執行當前PHP腳本所在路徑的configs文件夾下

1 <?php
2 $smarty->config_dir = 'lib/smarty/config';
3 ?>

 

setConfigDir(string|array config_dir);
另一種用來設定存儲配置文件目錄的方法

 1 <?php
 2 
 3 // set a single directory where the config files are stored
 4 $smarty->setConfigDir('./config');
 5 
 6 // view the config dir chain
 7 var_dump($smarty->getConfigDir());
 8 
 9 // set multiple directoríes where config files are stored
10 $smarty->setConfigDir(array(
11 'one' => './config',
12 'two' => './config_2',
13 'three' => './config_3',
14 ));
15 
16 // view the config dir chain
17 var_dump($smarty->getConfigDir());
18 
19 // chaining of method calls
20 $smarty->setTemplateDir('./templates')
21 ->setConfigDir('./config')
22 ->setCompileDir('./templates_c')
23 ->setCacheDir('./cache');
24 
25 ?>

addConfigDir(string|array config_dir, string key);
用來增加一個存儲配置文件的目錄

 1 <?php
 2  
 3 // add directory where config files are stored
 4 $smarty->addConigDir('./config_1');
 5 
 6 // add directory where config files are stored and specify array-key
 7 $smarty->addConfigDir('./config_1', 'one');
 8 
 9 // add multiple directories where config files are stored and specify array-keys
10 $smarty->addTemplateDir(array(
11 'two' => './config_2',
12 'three' => './config_3',
13 ));
14 
15 // view the template dir chain
16 var_dump($smarty->getConfigDir());
17 
18 // chaining of method calls
19 $smarty->setConfigDir('./config')
20 ->addConfigDir('./config_1', 'one')
21 ->addConfigDir('./config_2', 'two');
22 
23 ?>

 

getConfigDir()
獲取配置文件的路徑信息

 1 <?php
 2 
 3 // set some config directories
 4 $smarty->setConfigDir(array(
 5     'one' => './config',
 6     'two' => './config_2',
 7     'three' => './config_3',
 8 ));
 9 
10 // get all directories where config files are stored
11 $config_dir = $smarty->getConfigDir();
12 var_dump($config_dir); // array
13 
14 // get directory identified by key
15 $config_dir = $smarty->getConfigDir('one');
16 var_dump($config_dir); // string
17 
18 ?>

 

讀取配置文件:

  • 使用內置標簽 {config_load}

  在模版中調用該標簽,並指定該標簽的file屬性,既可獲取配置文件。file屬性接收要讀取配置文件的路徑和名稱,如:example.conf。
  可選參數section,為要獲取的配置文件中的指定塊區域。
  可選參數scope,其值只能為"local"、"parent"和"global",默認值"local",表示只能在當前模版中使用獲取到配置文件值;parent:可在當前模版與調用當前模版的父模版中使用;global:所有模版中都可以使用。

1 {config_load file='example.conf' section='Customer'}

 

  • 使用Smarty內置方法 void configLoad(string file, string section);

  該方法將會自動讀取配置文件內容並發送到模版中,並且scope屬性總是global。

1 <?php
2 // 讀取並分配my.conf配置文件
3 $smarty->configLoad('my.conf');
4  
5 // 讀取配置文件中的指定塊區域
6 $smarty->configLoad('my.conf', 'foobar');
7 ?>

 

獲取配置文件中的變量:
成功讀取配置文件后,就可獲取配置文件中的變量信息,在模版中獲取配置文件變量的方法是將變量名用"#"括起來,或者使用smarty變量$smarty.config

1 {*使用#*}
2 <body bgcolor="{#bodyBgColor#}">
3 {*使用$smarty.config*}
4 <body bgcolor="{$smarty.config.bodyBgColor}">

 

在PHP文件中,可以通過getConfigVars(sting varname)方法獲取配置文件變量。
如果未指定varname變量,則將所有的變量通過數組的形式返回

1 <?php
2 
3 // 獲取foo變量值
4 $myVar = $smarty->getConfigVars('foo');
5 
6 // 獲取所有變量值
7 $all_config_vars = $smarty->getConfigVars();
8 ?>

 

其它常用配置文件相關方法及變量
void clearConfig(string var);
用於清除給定的var變量,若為指定該變量,則清除所有變量。

1 <?php
2 // 清除所有分配的配置文件變量
3 $smarty->clearConfig();
4 
5 // 清除foobar變量
6 $smarty->clearConfig('foobar');
7 ?>

 

$default_config_handler_func
可將自定義方法名賦值給該變量,當無法獲取配置文件時,就會觸發自定義方法。

 1 <?php
 2 
 3 $smarty = new Smarty();
 4 $smarty->default_config_handler_func = 'my_default_config_handler_func';
 5 
 6 /**
 7 * Default Config Handler
 8 *
 9 * called when Smarty's file: resource is unable to load a requested file
10 * 
11 * @param string $type resource type (e.g. "file", "string", "eval", "resource")
12 * @param string $name resource name (e.g. "foo/bar.tpl")
13 * @param string &$content config's content
14 * @param integer &$modified config's modification time
15 * @param Smarty $smarty Smarty instance
16 * @return string|boolean path to file or boolean true if $content and $modified 
17 * have been filled, boolean false if no default config 
18 * could be loaded
19 */
20 function my_default_config_handler_func($type, $name, &$content, &$modified, Smarty $smarty) {
21 if (false) {
22  // return corrected filepath
23 return "/tmp/some/foobar.tpl";
24 } elseif (false) {
25 // return a config directly
26 $content = 'someVar = "the config source"';
27 $modified = time();
28 return true;
29 } else {
30 // tell smarty that we failed
31 return false;
32 }
33 }
34 
35 ?>

 

$config_overwrite
如果該值為ture,若讀取的配置文件變量中存在同名變量,后出現的變量值將會覆蓋前面的同名變量值,默認為true。
如果設置為false,將不會覆蓋任何只,而是將同名變量保存到數組中返回。

string compileAllConfig(string extension, boolean force, integer timelimit, integer maxerror);
該方法用於編譯在$config_dir目錄中獲取到的所有配置文件

  • extension 可選參數,用於定義配置文件的后綴名,默認是".conf"
  • force 可選參數,如果該值為false,則只有自上次編譯后修改過的配置文件才會從新編譯;如果為ture,則所有文件都會被編譯
  • timelimit 可選參數,用於限制編譯過程所需要的時間,以秒為單位的整數值
  • maxerror 可選參數,用於設定當超過多少個配置文件編譯失敗時,停止編譯過程

$config_booleanize
如果將該變量設定為true(默認),配置文件中屬性值為on/true/yes和off/false/no的值,將會被自動轉換成bool值true和false。
$config_read_hidden
如果將該變量設定為true,則能夠讀取配置文件中的隱藏域。隱藏域是點一個"."開頭,如上面的[.Database],該值默認為false。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM