<table cellspacing="0" cellpadding="0" width="100%" summary="Header navigation table" border="0"><tbody><tr><th align="middle" colspan="3">Smarty - the compiling PHP template engine</th></tr><tr><td valign="bottom" align="left" width="25%"><a accesskey="P" href="appendixes.html">Prev</a></td> <td valign="bottom" align="middle" width="50%"/> <td valign="bottom" align="right" width="25%"><a accesskey="N" href="tips.html">Next</a></td></tr></tbody></table>
# Chapter 17. Troubleshooting
第十七章. 疑难解答
**Table of Contents**[Smarty/PHP errors](#)[错误]
# Smarty/PHP errors[错误]
Smarty can catch many errors such as missing tag attributes or malformed variable names. If this happens,you will see an error similar to the following:
Smarty能够捕获许多错误,比如缺少标签属性或者不规范变量名。如果发生这种错误,就会有类似下面的错误提示:
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td> <div class="EXAMPLE"><a name="AEN4189"/> <b>Example 17.1. Smarty errors<br/> 例17-1.Smarty 错误</b> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="PROGRAMLISTING">Warning: Smarty: [in index.tpl line 4]: syntax error: unknown tag - '%blah'
in /path/to/smarty/Smarty.class.php on line 1041警告:Smarty: 在index.tpl文件第4行,语法错误:'%blah'标签未知
Fatal error: Smarty: [in index.tpl line 28]: syntax error: missing section name
in /path/to/smarty/Smarty.class.php on line 1041严重错误:Smarty: 在index.tpl文件第28行,语法错误:缺少节段名字在 /path/to/smarty/路径中的Smarty.class.php文件1041行</pre></td></tr></tbody></table></div></td></tr></tbody></table>
Smarty shows you the template name, the line number and the error. After that, the error consists of the actual line number in the Smarty class that the error occured.
There are certain errors that Smarty cannot catch, such as missing close tags. These types of errors usually end up in PHP compile-time parsing errors.
Smarty可以显示模板名称以及行号和错误。后面跟着包含这些错误在smarty类所在的实际行号。
Smarty不能捕获某些错误,像缺少结束标签。这类型的错误一般以php编译时解析错误终止。
<table class="EXAMPLE" cellspacing="0" cellpadding="0" width="100%" border="0"><tbody><tr><td> <div class="EXAMPLE"><span class="PROGRAMLISTING"><strong>Example 17.2. PHP parsing errors</strong></span><br/><strong>例17-2.php解析错误</strong><table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="PROGRAMLISTING">Parse error: parse error in /path/to/smarty/templates_c/index.tpl.php on line 75</pre></td></tr></tbody></table></div></td></tr></tbody></table>
When you encounter a PHP parsing error, the error line number will correspond to the compiled PHP script, NOT the template itself. Usually you can look at the template and spot the syntax error. Here are some common things to look for: missing close tags for {if}{/if} or {section}{/section} , or syntax of logic within an {if} tag. If you can't find the error, you might have to open the compiled PHP file and go to the line number to figure out where the corresponding error is in the template.
当你遇到一个php解析错误时,错误行号反映的php编译脚本,而不是模板本身(虽然实际情况是模板有误,但错误机制只反映在php编译脚本)。一般情况下,你只须查看模板并发现错误语法。这里提供一些常见的错误:缺少{if}{/if}或者{section}{/section}的闭合标签,或者{if}标签内的逻辑语法错误。如果你不能在模板中发现错误,那就可能需要打开php编译文件,然后查看行号,并返回模板找出相应的错误。
**Example 17.3. Other common errors**
**例17-3.其他常见错误**
<table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="PROGRAMLISTING">Warning: Smarty error: unable to read resource: "index.tpl" in...orWarning: Smarty error: unable to read resource: "site.conf" in...</pre> <li>The $template_dir is incorrect, doesn't exist or the file index.tpl is not in the templates/directory</li> <li>$template_dir不正确,不存在或index.tpl文件不在templates/目录下</li> <li>A {config_load} function is within a template (or config_load() has been called) and either $config_dir is incorrect, does not exist or site.conf is not in the directory.</li> <li>{config_load}函数在模板中(或调用了configLoad()),并且$config_dir不正确,不存在或site.conf不在目录中。<br/><br/><br/> Fatal error: Smarty error: the $compile_dir 'templates_c' does not exist,or is not a directory...<br/><br/></li> <li>Either the $compile_dir is incorrectly set, the directory does not exist, or templates_c is a file and not a directory.</li> <li> 要么$compile_dir设置错误,目录不存在,要么templates_c只是个文件,而非目录。<br/><br/><br/> Fatal error: Smarty error: unable to write to $compile_dir '....<br/><br/></li> <li>The $compile_dir is not writable by the web server. See the bottom of the installing smarty page for more about permissions.</li> <li> $compile_dir在服务器中不可写。关于权限浏览Smarty安装页面底部。<br/><br/><br/> Fatal error: Smarty error: the $cache_dir 'cache' does not exist,or is not a directory. in /..<br/><br/></li> <li>This means that $caching is enabled and either; the $cache_dir is incorrectly set, the directory does not exist, or cache/ is a file and not a directory.</li> <li> 这意味着$caching被激活同时$cache_dir设置错误、目录不存在或者cache/是一个文件而非目录。<br/><br/><br/> Fatal error: Smarty error: unable to write to $cache_dir '/...<br/><br/></li> <li>This means that $caching is enabled and the $cache_dir is not writable by the web server. See the bottom of the installing smarty page for permissions.</li> <li> 这意味着$caching被激活同时$cache_dir在服务器中不可写。关于权限浏览Smarty安装页面底部。</li></td> </tr></tbody></table>
参见[debugging](#)。
<table cellspacing="0" cellpadding="0" width="100%" summary="Footer navigation table" border="0"><tbody><tr><td valign="top" align="left" width="33%"><a accesskey="P" href="appendixes.html">Prev</a></td> <td valign="top" align="center" width="34%"><a accesskey="H" href="index.html">Home</a></td> <td valign="top" align="right" width="33%"><a accesskey="N" href="tips.html">Next</a></td></tr><tr><td valign="top" align="left" width="33%"> Appendixes<br/>附录 </td> <td valign="top" align="center" width="34%"><a accesskey="U" href="appendixes.html">Up</a></td> <td valign="top" align="right" width="33%"> Tips & Tricks<br/>使用技巧和经验 </td></tr></tbody></table>
- Smarty模板编译引擎
- 序
- 译序
- I.开始
- 第一章. 什么是Smarty?
- 第二章. 安装
- II.模板设计者篇
- 第三章.基本语法
- 注释
- 变量
- 函数
- 属性
- 双引号里嵌入变量
- 数学运算
- 忽略Smarty解析
- 第四章.变量
- 从PHP分配的变量
- 从配置文件读取的变量
- 变量范围
- {$smarty}保留变量
- 第五章.变量调节器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.组合修改器
- 第七章.内置函数
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定义函数
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.调试控制台
- III.模板程序员篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty类变量
- $template_dir
- 第十三章.Smarty类方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.缓存
- 建立缓存
- 多重缓存
- 缓存集合
- 控制插件输出的可缓存性
- 第十五章.高级特性
- 安全
- 通过模板更改设置
- 模板继承
- 数据流
- 对象
- 静态类
- 预过滤器
- 后过滤器
- 输出过滤器
- 缓存处理函数
- 资源
- 第十六章.以插件扩展Smarty
- 插件如何工作
- 命名约定
- 编写插件
- 模板函数
- 调节器
- 块函数
- 编译函数
- 预滤器/后滤器
- 输出过滤器
- 资源
- 插入
- Ⅳ.附录
- 第十七章.疑难解答
- Smarty/PHP 错误
- 第十八章.使用技巧和经验
- 空白变量处理
- 默认变量处理
- 传递变量标题给头模板
- 日期
- WAP/WML
- 组件化模板
- 拒绝电子邮件地址
- 第十九章. 相关资源
- 第二十章. 漏洞
- 3.0安装包
- 2.x版本升级至3.x版本的提示
- 3.0.x使用指南
- 翻译人员列表