编写smarty配置文件


配置文件有利于设计者管理文件中的模板全局变量。
最简单的例子就是模板色彩变量。一般情况下如果想改变一个程序的外观色彩,你就必须通过去更改每

一个文件的颜色变量。如果有这个配置文件的话,色彩变量就可以保存在一个地方,只要改变这个配置

文件就可以实现你色彩的更新。还可以进行设置,只有查看没有设置的权限
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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值