Smarty 是什么?
为何使用 Smarty?
用例和工作流程
语法对比
模板继承
最佳实践
速成课程
{config_load} 用于从配置文件 中加载 config #variables# 到模板中。
属性
| 属性名称 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
| file | 字符串 | 是 | 无 | 要包含的配置文件的名称 |
| section | 字符串 | 否 | 无 | 要加载的区段名称 |
| scope | 字符串 | 否 | local | 加载的变量的作用域的处理方式,它必须是 local、parent 或 global 中的一个。local 表示变量加载到本地模板上下文中。parent 表示变量加载到本地上下文和调用它的父模板中。global 表示所有模板都可以使用变量。 |
示例 7.24 {config_load}
example.conf 文件。
#this is config file comment # global variables pageTitle = "Main Menu" bodyBgColor = #000000 tableBgColor = #000000 rowBgColor = #00ff00 #customer variables section [Customer] pageTitle = "Customer Info"
和模板
{config_load file="example.conf"}
{config_load "example.conf"} {* short-hand *}
<html>
<title>{#pageTitle#|default:"No title"}</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>
配置文件 也可以包含区段。您可以通过添加 section 属性从区段中加载变量。请注意,全局 config 变量始终与区段变量一起加载,并且同名区段变量会覆盖全局变量。
配置文件的区段和名为 {section} 的内置模板函数是无关的,它们碰巧有相同的命名约定。
示例 7.25 function {config_load} 与区段
{config_load file='example.conf' section='Customer'}
{config_load 'example.conf' 'Customer'} {* short-hand *}
<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>
请参阅 $config_overwrite 以创建配置文件变量的数组。
请参阅以下页面:配置文件、config 变量、$config_dir、getConfigVars() 和 configLoad()。