无需在php程序中分配,但和保留变量不一样,它是通过配置文件配置的。
configs/site.conf(配置文件):
copyright = "1500~2015 嵩山少林寺有限公司 版权所有" ;后面的值加不加引号都可以,但如果值有空格的话一般要加引号。
police = 京公网安备100003214号
;国庆节 (配置节)
[nationality]
color = #ff0000
;元旦 (配置节)
[newyear]
color = #00ff00
config.php(后端):
<?php
include "libs/Smarty.class.php";
$smarty = new Smarty();
$smarty->template_dir = "templates";
$smarty->compile_dir = "templates_c";
$smarty->display("config.tpl");
config.tpl(前端视图):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
</head>
<body>
<h2>配置变量</h2>
{config_load file = 'site.conf' section='nationality'} {* 载入配置文件,指定配置节 *}
<p>{#copyright#}</p> {* 引用配置变量的第一种方式 *}
<p>{$smarty.config.police}</p> {* 引用配置变量的第二种方式 *}
<p>{#color#}</p> {* 配置文件中如果配置有多个color,那么就需要指定配置节(指定是哪个配置节下的color) *}
</body>
</html>