Skip to content

从配置文件加载的变量

配置文件加载的变量通过将其包含在 #hash_marks# 中引用,或使用 Smarty 变量 $smarty.config。后一种语法对于嵌入到引号括起来的属性值中或访问变量值(例如 $smarty.config.$foo)非常有用。

示例

示例配置文件 - foo.conf

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

使用 #hash# 方法的模板:

{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>

使用 $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>

这两个示例都将输出:

<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}中解释了这个过程。

另请参见 variables$smarty 保留变量