Smarty 是什么?
为什么要使用它?
用例和工作流程
语法比较
模板继承
最佳实践
速成课程
Smarty 可以捕获许多错误,例如缺少标签属性或格式错误的变量名称。如果发生这种情况,您将看到类似于以下内容的错误
示例 20.1。Smarty 错误
Warning: Smarty: [in index.tpl line 4]: syntax error: unknown tag - '%blah'
in /path/to/smarty/Smarty.class.php on line 1041
Fatal error: Smarty: [in index.tpl line 28]: syntax error: missing section name
in /path/to/smarty/Smarty.class.php on line 1041
Smarty 向您展示模板名称、行号和错误。然后,错误由发生错误的 Smarty 类中的实际行号组成。
还有一些错误是 Smarty 无法捕获的,例如缺少关闭标签。这些类型的错误通常最终导致 PHP 编译时解析错误。
当您遇到 PHP 解析错误时,错误行号将对应于编译后的 PHP 脚本,而不是模板本身。通常,您可以查看模板并找到语法错误。以下是一些常见的需要注意的事项:缺少 {if}{/if} 或 {section}{/section} 的关闭标签,或者 {if} 标签中逻辑的语法。如果您找不到错误,您可能需要打开编译后的 PHP 文件并转到行号以找出模板中相应错误的位置。
示例 20.3。其他常见错误
Warning: Smarty error: unable to read resource: "index.tpl" in... or Warning: Smarty error: unable to read resource: "site.conf" in...
$template_dir 不正确,不存在或文件 index.tpl 不在 templates/ 目录中
{config_load} 函数位于模板中(或 configLoad() 已被调用),并且 $config_dir 不正确,不存在或 site.conf 不在该目录中。
Fatal error: Smarty error: the $compile_dir 'templates_c' does not exist,
or is not a directory...
$compile_dir 设置不正确,该目录不存在,或者 templates_c 是一个文件,而不是目录。
Fatal error: Smarty error: unable to write to $compile_dir '....
$compile_dir 未被网络服务器赋予写入权限。参阅 安装 Smarty 页面底部的权限信息。
Fatal error: Smarty error: the $cache_dir 'cache' does not exist,
or is not a directory. in /..
这意味着启用 $caching 时, $cache_dir 错误设置、目录不存在或 cache/ 是一个文件而不是目录。
Fatal error: Smarty error: unable to write to $cache_dir '/...
这意味着启用 $caching 时,网络服务器未授予 $cache_dir 写入权限。参阅 安装 Smarty 页面底部的权限信息。
Warning: filemtime(): stat failed for /path/to/smarty/cache/3ab50a623e65185c49bf17c63c90cc56070ea85c.one.tpl.php in /path/to/smarty/libs/sysplugins/smarty_resource.php
这意味着您的应用程序注册了一个自定义错误处理程序(使用 set_error_handler()),它不尊重给定的 $errno。如果由于某些原因,这是您指定的自定义错误处理程序的行为,请在您注册自定义错误处理程序之后调用 muteExpectedErrors()。
另请参阅 调试。