编写smarty配置文件
2012-11-08 19:36
190 查看
配置文件有利于设计者管理文件中的模板全局变量。
最简单的例子就是模板色彩变量。一般情况下如果想改变一个程序的外观色彩,你就必须通过去更改每
一个文件的颜色变量。如果有这个配置文件的话,色彩变量就可以保存在一个地方,只要改变这个配置
文件就可以实现你色彩的更新。还可以进行设置,只有查看没有设置的权限
config_load()加载配置
file待包含的配置文件的名称
section 配置文件中待加载部分的名称
一、色彩例子
把配置代码写在以.conf命名的记事本中.配置文件以#作为注释,[]中表局部变量,配置文件中结尾不用
写“;”。
1、a.conf
#全局变量
bgColor=#004c
display=true #显示数据,等于false时不显示数据,在全局中默认是true
#局部变量
[mycolor]
bgColor=#cccc00
[youColor]
bgColor=#cc0
2、peizhi.php
<?php
include("libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="demo/templates";
$smarty->compile_dir="demo/tempaltes_c";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->config_dir="demo/config";//配置文件存放路径
$arr=array(
array("id"=>"1","name"=>"张三","age"=>18),
array("id"=>"2","name"=>"李四","age"=>"19")
);
$smarty->assign("arr",$arr);
$smarty->display("peizhi.tpl");
?>
3、peizhi.tpl
<{config_load file="a.conf" section="mycolor"}><{*session是config_load的一个属性,配置文件
中待加载部分的名称*}>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<body bgcolor="<{#bgColor#}>">
<{if #display#}>
<table width="200" border="1">
<caption>foreach遍历全部</caption>
<th>id</th><th>name</th><th>age</th>
<{foreach from=$arr item=row }><!--row=$row[0],$row[1]..,获得外层所有的数组,都遍历出来-->
<tr>
<{foreach from=$row item=value}><!--$row是一个数组,在获得里面的值,value=1,张三,18-->
<td><{$value}></td>
<{/foreach}>
</tr>
<{/foreach}>
</table>
<{else}>请把配置文件的false改为true
<{/if}>
</body>
</html>
这个是显示局部变量[mycolor]他的颜色,display=true显示数据,如果在.conf中没有设置display默认是false
其他条件不变,如果display=false,(或者不设置display),将不会显示数据。
如果把peizhi.tpl中第一行section=“myColor”去掉,就会显示全局变量的颜色bgColor=#004c
最简单的例子就是模板色彩变量。一般情况下如果想改变一个程序的外观色彩,你就必须通过去更改每
一个文件的颜色变量。如果有这个配置文件的话,色彩变量就可以保存在一个地方,只要改变这个配置
文件就可以实现你色彩的更新。还可以进行设置,只有查看没有设置的权限
config_load()加载配置
file待包含的配置文件的名称
section 配置文件中待加载部分的名称
一、色彩例子
把配置代码写在以.conf命名的记事本中.配置文件以#作为注释,[]中表局部变量,配置文件中结尾不用
写“;”。
1、a.conf
#全局变量
bgColor=#004c
display=true #显示数据,等于false时不显示数据,在全局中默认是true
#局部变量
[mycolor]
bgColor=#cccc00
[youColor]
bgColor=#cc0
2、peizhi.php
<?php
include("libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="demo/templates";
$smarty->compile_dir="demo/tempaltes_c";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->config_dir="demo/config";//配置文件存放路径
$arr=array(
array("id"=>"1","name"=>"张三","age"=>18),
array("id"=>"2","name"=>"李四","age"=>"19")
);
$smarty->assign("arr",$arr);
$smarty->display("peizhi.tpl");
?>
3、peizhi.tpl
<{config_load file="a.conf" section="mycolor"}><{*session是config_load的一个属性,配置文件
中待加载部分的名称*}>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<body bgcolor="<{#bgColor#}>">
<{if #display#}>
<table width="200" border="1">
<caption>foreach遍历全部</caption>
<th>id</th><th>name</th><th>age</th>
<{foreach from=$arr item=row }><!--row=$row[0],$row[1]..,获得外层所有的数组,都遍历出来-->
<tr>
<{foreach from=$row item=value}><!--$row是一个数组,在获得里面的值,value=1,张三,18-->
<td><{$value}></td>
<{/foreach}>
</tr>
<{/foreach}>
</table>
<{else}>请把配置文件的false改为true
<{/if}>
</body>
</html>
这个是显示局部变量[mycolor]他的颜色,display=true显示数据,如果在.conf中没有设置display默认是false
其他条件不变,如果display=false,(或者不设置display),将不会显示数据。
如果把peizhi.tpl中第一行section=“myColor”去掉,就会显示全局变量的颜色bgColor=#004c
相关文章推荐
- JavaWeb之JDBC(二)采用读取配置文件方式编写JDBC的工具类,获取数据库的连接
- Hibernate的学习之路十九(一对多的JavaBean编写和映射的配置文件的编写)
- 如何编写训练测试的prototxt配置文件---以Resnet为例
- nginx启动配置文件编写
- caffe系列之:Blob,Layer and Net以及对应配置文件的编写
- Smarty的配置文件
- Grails 项目配置数据连接的配置文件的编写 Grails连接Oracle grails连接Mysql 的配置文件
- 从0搭建pythonseleniumweb框架之第一步编写配置文件,浏览器引擎类,日志类
- Caffe学习系列(6):Blob,Layer and Net以及对应配置文件的编写
- smarty配置文件
- C++编写Config类读取配置文件
- Caffe学习系列(6):Blob,Layer and Net以及对应配置文件的编写
- 在配置文件中编写代码,不能自动提示问题
- 用STL快速编写ini配置文件识别类
- C++编写Config类读取配置文件
- Hibernate无主键配置文件编写
- SSM整合之配置文件的编写
- eclipse中在框架开发时编写xml等配置文件的时候,引号中的包、路径等文件不提示的解决方法
- Struts2中配置文件编写简介
- Hibernate的学习之路十九(一对多的JavaBean编写和映射的配置文件的编写)