您的位置:首页 > 其它

Smarty 学习笔记四 配置文件的读取

2012-06-12 20:04 561 查看
设置配置文件
Smarty配置文件用于存放全局变量,例如将模版颜色存放到配置文件中,这样,当修改模版颜色时,不需逐个的去修改每个模版,而是直接修改配置文件既可。配置文件格式如下:

# global variables
pageTitle = "Main Menu"
bodyBgColor = #000000
tableBgColor = #000000
rowBgColor = #00ff00

[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."""

# hidden section
[.Database]
host=my.example.com
db=ADDRESSBOOK
user=php-user
pass=foobar


配置文件支持注释,只需在要注释的行开始处使用#既可。
也可将配置文件分为多个块,在不同的模版中使用不同的配置文件块区域。块名由"[]"括起来,名称可以是任意不包含"[" 和 "]"的字符串。如上例中的[Customer]和[Login]。
上例配置文件中,前4个变量不属于任何块,读取配置文件时,它们将总被获取到,即使是读取指定块区域的配置文件。如果块区域中的变量名与全局变量名重名,并且$config_overwrite为true时,块区域变量值将会替换全局变量的值。
配置文件路径
在使用配置文件之前,需要先指定配置文件所在路径,可通过下列方法实现
$config_dir
用来定义存放配置文件的路径信息,默认路径是./configs,即执行当前PHP脚本所在路径的configs文件夹下

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


setConfigDir(string|array config_dir);
另一种用来设定存储配置文件目录的方法

<?php

// set a single directory where the config files are stored
$smarty->setConfigDir('./config');

// view the config dir chain
var_dump($smarty->getConfigDir());

// set multiple directoríes where config files are stored
$smarty->setConfigDir(array(
'one' => './config',
'two' => './config_2',
'three' => './config_3',
));

// view the config dir chain
var_dump($smarty->getConfigDir());

// chaining of method calls
$smarty->setTemplateDir('./templates')
->setConfigDir('./config')
->setCompileDir('./templates_c')
->setCacheDir('./cache');

?>


addConfigDir(string|array config_dir, string key);
用来增加一个存储配置文件的目录

<?php

// add directory where config files are stored
$smarty->addConigDir('./config_1');

// add directory where config files are stored and specify array-key
$smarty->addConfigDir('./config_1', 'one');

// add multiple directories where config files are stored and specify array-keys
$smarty->addTemplateDir(array(
'two' => './config_2',
'three' => './config_3',
));

// view the template dir chain
var_dump($smarty->getConfigDir());

// chaining of method calls
$smarty->setConfigDir('./config')
->addConfigDir('./config_1', 'one')
->addConfigDir('./config_2', 'two');

?>


getConfigDir()
获取配置文件的路径信息

<?php

// set some config directories
$smarty->setConfigDir(array(
'one' => './config',
'two' => './config_2',
'three' => './config_3',
));

// get all directories where config files are stored
$config_dir = $smarty->getConfigDir();
var_dump($config_dir); // array

// get directory identified by key
$config_dir = $smarty->getConfigDir('one');
var_dump($config_dir); // string

?>


读取配置文件:

使用内置标签 {config_load}

  在模版中调用该标签,并指定该标签的file属性,既可获取配置文件。file属性接收要读取配置文件的路径和名称,如:example.conf。
  可选参数section,为要获取的配置文件中的指定块区域。
  可选参数scope,其值只能为"local"、"parent"和"global",默认值"local",表示只能在当前模版中使用获取到配置文件值;parent:可在当前模版与调用当前模版的父模版中使用;global:所有模版中都可以使用。

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


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

  该方法将会自动读取配置文件内容并发送到模版中,并且scope属性总是global。

<?php
// 读取并分配my.conf配置文件
$smarty->configLoad('my.conf');

// 读取配置文件中的指定块区域
$smarty->configLoad('my.conf', 'foobar');
?>


获取配置文件中的变量:
成功读取配置文件后,就可获取配置文件中的变量信息,在模版中获取配置文件变量的方法是将变量名用"#"括起来,或者使用smarty变量$smarty.config。

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


在PHP文件中,可以通过getConfigVars(sting varname)方法获取配置文件变量。
如果未指定varname变量,则将所有的变量通过数组的形式返回

<?php

// 获取foo变量值
$myVar = $smarty->getConfigVars('foo');

// 获取所有变量值
$all_config_vars = $smarty->getConfigVars();
?>


其它常用配置文件相关方法及变量
void clearConfig(string var);
用于清除给定的var变量,若为指定该变量,则清除所有变量。

<?php
// 清除所有分配的配置文件变量
$smarty->clearConfig();

// 清除foobar变量
$smarty->clearConfig('foobar');
?>


$default_config_handler_func
可将自定义方法名赋值给该变量,当无法获取配置文件时,就会触发自定义方法。

<?php

$smarty = new Smarty();
$smarty->default_config_handler_func = 'my_default_config_handler_func';

/**
* Default Config Handler
*
* called when Smarty's file: resource is unable to load a requested file
*
* @param string $type resource type (e.g. "file", "string", "eval", "resource")
* @param string $name resource name (e.g. "foo/bar.tpl")
* @param string &$content config's content
* @param integer &$modified config's modification time
* @param Smarty $smarty Smarty instance
* @return string|boolean path to file or boolean true if $content and $modified
* have been filled, boolean false if no default config
* could be loaded
*/
function my_default_config_handler_func($type, $name, &$content, &$modified, Smarty $smarty) {
if (false) {
// return corrected filepath
return "/tmp/some/foobar.tpl";
} elseif (false) {
// return a config directly
$content = 'someVar = "the config source"';
$modified = time();
return true;
} else {
// tell smarty that we failed
return false;
}
}

?>


$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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: