配置 Smarty
设置模板路径
默认情况下,Smarty 在 ./templates
中查找要呈现的模板。
您可以更改此设置,甚至可以在查找模板时使用多个路径。
如果您需要更改此设置,可以使用 setTemplateDir()
或 addTemplateDir()
。使用 getTemplateDir()
检索已配置的路径。
<?php
// set a single directory where the config files are stored
$smarty->setTemplateDir('./templates');
// set multiple directories where templates are stored
$smarty->setTemplateDir(['./templates', './templates_2', './templates_3']);
// add directory where templates files are stored to the current list of dirs
$smarty->addTemplateDir('./templates_1');
// add multiple directories to the current list of dirs
$smarty->addTemplateDir([
'./templates_2',
'./templates_3',
]);
// chaining of method calls
$smarty->setTemplateDir('./templates')
->addTemplateDir('./templates_1')
->addTemplateDir('./templates_2');
// insert a template dir before exising template dirs
$smarty->prependTemplateDir('./more_important_templates')
// get all directories where config files are stored
$template_dirs = $smarty->getTemplateDir();
var_dump($template_dirs); // array
// get directory identified by key
$template_dir = $smarty->getTemplateDir(0);
var_dump($template_dir); // string
设置已编译模板的路径
Smarty 将模板编译为原生 PHP,以实现最快的速度。存储这些 PHP 文件的默认路径是 ./templates_c
。
如果您需要更改此设置,可以使用 setCompileDir()
。使用 getCompileDir()
检索已配置的路径。
<?php
// set another path to store compiled templates
$smarty->setCompileDir('/data/compiled_templates');
// get directory where compiled templates are stored
$compileDir = $smarty->getCompileDir();
设置配置文件路径
Smarty 可以 从配置文件中加载数据。默认情况下,Smarty 从 ./configs
加载配置文件。
您可以更改此设置,甚至可以在查找配置文件时使用多个路径。
如果您需要更改此设置,可以使用 setConfigDir()
或 addConfigDir()
。使用 getConfigDir()
检索已配置的路径。
<?php
// set a single directory where the config files are stored
$smarty->setConfigDir('./config');
// set multiple directories where config files are stored
$smarty->setConfigDir(['./config', './config_2', './config_3']);
// add directory where config files are stored to the current list of dirs
$smarty->addConfigDir('./config_1');
// add multiple directories to the current list of dirs
$smarty->addConfigDir([
'./config_2',
'./config_3',
]);
// chaining of method calls
$smarty->setConfigDir('./config')
->addConfigDir('./config_1', 'one')
->addConfigDir('./config_2', 'two');
// get all directories where config files are stored
$config_dirs = $smarty->getConfigDir();
var_dump($config_dirs); // array
// get directory identified by key
$config_dir = $smarty->getConfigDir(0);
var_dump($config_dir); // string
设置缓存路径
尽管 Smarty 以原生 PHP 运行模板以实现最高速度,但它仍然需要在每次调用时执行 PHP 代码。如果您的数据不是经常发生改变,则可以使用输出缓存来进一步加快应用程序的速度。
输出缓存可能是一个棘手的问题,所以我们专门为 缓存 编写了完整一节。如果您要使用缓存,务必阅读该部分内容。
默认情况下,Smarty 将缓存存储到名为 ./cache
的子目录中的 PHP 文件中。
若需要更改,可以使用 setCacheDir()
。使用 getCacheDir()
检索已配置的路径。
<?php
// set another path to store caches
$smarty->setCacheDir('/data/caches');
// get directory where cached templates are stored
$cacheDir = $smarty->getCacheDir();
启用自动转义
默认情况下,Smarty 不会转义在模板中呈现的任何内容。如果你使用 Smarty 呈现 HTML 页面,这意味着你必须确保不会呈现任何在 HTML 中具有特殊含义的字符,例如 &
、<
和 >
,或将转义修饰语应用到你想要呈现的所有内容。
如果你忘记这么做,你可能会破坏你的 HTML 页面,甚至会创建名为XSS 或跨站脚本的漏洞。
所幸,你可以让 Smarty 自动将转义修饰语应用到模板的任何动态部分。就像 Smarty 在你网页上使用的每个变量中都神奇地添加了 |escape
一样。
如下启用 HTML 的自动转义
启用自动转义后,|escape
修饰符的默认模式(html
)不起作用,以避免重复转义。可以使用 force
模式强制执行它。其他模式(htmlall
、url
、urlpathinfo
、quotes
、javascript
)可以按预期结果使用,而不会重复转义。
即使启用自动转义,你可能仍希望在不转义的情况下显示变量的内容。要这么做,请使用 |raw
修饰符。
示例(启用自动转义后)
{* these three statements are identical *}
{$myVar}
{$myVar|escape}
{$myVar|escape:'html'}
{* no double-escaping on these statements *}
{$var|escape:'htmlall'}
{$myVar|escape:'url'}
{$myVar|escape:'urlpathinfo'}
{$myVar|escape:'quotes'}
{$myVar|escape:'javascript'}
{* no escaping at all *}
{$myVar|raw}
{* force double-escaping *}
{$myVar|escape:'force'}
禁用编译检查
默认情况下,Smarty 测试当前模板是否已在最新一次编译后发生更改。如果已更改,则会重新编译该模板。
一旦将应用程序投入生产,通常不再需要这个编译检查步骤,而且额外的检查会显著降低性能。务必在生产中禁用编译检查,以获得最大的性能。
如果启用了缓存
并且启用了编译检查,那么如果涉及的模板文件或配置文件已更新,则会重新生成缓存文件。
字符集编码
文本数据有多种编码方式,ISO-8859-1(Latin1)和 UTF-8 是最流行的。除非你更改 \Smarty\Smarty::$_CHARSET
,否则 Smarty 将 UTF-8
识别为内部字符集。
注意
ISO-8859-1
从一开始就是 PHP 的默认内部字符集。自 1991 年以来,Unicode 一直在发展。此后,它已成为征服所有字符集的唯一字符集,因为它能够对大多数已知字符进行编码,甚至包括跨越不同字符系统(拉丁语、西里尔语、日语,...)的字符。UTF-8
是 Unicode 使用最多的编码,因为它允许以尽可能小的开销引用数千个字符。由于 unicode 和 UTF-8 现已非常广泛,强烈建议使用它们。
注意
自 Smarty 3.1 以来,Smarty 的内部和核心插件真正地兼容 UTF-8。