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。

原文地址:https://www.cnblogs.com/gbyukg/p/2539067.html