<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'&#13; in /path/to/smarty/Smarty.class.php on line 1041警告:Smarty: 在index.tpl文件第4行,语法错误:'%blah'标签未知&#13; &#13; Fatal error: Smarty: [in index.tpl line 28]: syntax error: missing section name&#13; 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 &amp; Tricks<br/>使用技巧和经验 </td></tr></tbody></table>