跳转至正文

从配置文件加载的变量

配置文件加载的变量通过将它们用 #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} 进行了说明。

另请参见 变量$smarty 保留变量