smarty变量--从配置文件读取的变量

加载配置文件后,配置文件中的变量需要用两个井号"#"包围或者是smarty的保留变量$smarty.config.来调用(下节将讲到),第二种语法在变量作为属性值嵌入至引号的时候非常有用,详细可参考双引号里值的嵌入

译注

(译注:举个例子 {include file="#includefile#"} 这样#includefile#将被当作字符处理,而不表示配置文件变量,但可以这样表示 {include file="`$smarty.config.includefile`"}不要忘了加``,当然也可用{$smarty.config.includefile}表示)。

例 4-7.从配置文件引用的变量

config file - foo.conf:

pageTitle = "This is mine"
bodyBgColor = '#eeeeee'
tableBorderSize = 3
tableBgColor = "#bbbbbb"
rowBgColor = "#cccccc"

A template demonstrating the #hash# method:
//下面为配置文件变量“井号方法”的示例
{config_load file='foo.conf'}
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>

A template demonstrating the $smarty.config method:
//下面为$smarty.config方法的示例:

{config_load file='foo.conf'}
<html>
<title>{$smarty.config.pageTitle}</title>
<body bgcolor="{$smarty.config.bodyBgColor}">
<table border="{$smarty.config.tableBorderSize}" bgcolor="{$smarty.config.tableBgColor"}>
<tr bgcolor="{$smarty.config.rowBgColor}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>


Both examples would output:


<html>
<title>This is mine</title>
<body bgcolor="#eeeeee">
<table border="3" bgcolor="#bbbbbb">
<tr bgcolor="#cccccc">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>  

配置文件的变量只有在它们被加载以后才能使用,这个过程将在以后{config_load}的章节里说明。

posted @ 2012-03-08 11:57  haiwei.sun  阅读(299)  评论(0编辑  收藏  举报
返回顶部