您的位置:首页 > 其它

编写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

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