<table cellspacing="0" cellpadding="0" border="0" width="100%" summary="Header navigation table"><tbody><tr><th align="center" colspan="3">Smarty - the compiling PHPtemplate engine</th> </tr><tr><td valign="bottom" align="left" width="25%"><a accesskey="P" href="api.register.resource.html">Prev</a></td> <td valign="bottom" align="center" width="50%">Chapter 13. Smarty Class Methods()</td> <td valign="bottom" align="right" width="25%"><a accesskey="N" href="api.unregisterfilter.html">Next</a></td> </tr></tbody></table> # templateExists()[模板是否存在] Name templateExists() ― checks whether the specified template exists 检查指定的模板是否存在 Description bool templateExists(string template); It can accept either a path to the template on the filesystem or a resource string specifying the template. 它既可接收模板的文件路径,也可接收指定的资源字符串作为参数。 **Example 13.31. templateExists()** <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><p> This example uses $_GET['page'] to {include} a content template. If the template does not exist then an error page is displayed instead. </p> <p>本例使用$_GET['page']去{include}包含一个内容模板。如果模板不存在,则显示一个错误提示页面。</p> <p>First the page_container.tpl<br/> &lt;html&gt;<br/> &lt;head&gt;&lt;title&gt;{$title}&lt;/title&gt;&lt;/head&gt;<br/> &lt;body&gt;<br/> {include file='page_top.tpl'}<br/> {* include middle content page *}<br/> {include file=$content_template}<br/> {include file='page_footer.tpl'}<br/> &lt;/body&gt;<br/><br/> And the php script //php脚本 </p> <p>&lt;?php<br/> // set the filename eg index.inc.tpl<br/> $mid_template = $_GET['page'].'.inc.tpl';<br/> if( !$smarty-&gt;templateExists($mid_template) ){<br/> $mid_template = 'page_not_found.tpl';<br/> }<br/> $smarty-&gt;assign('content_template', $mid_template);<br/> $smarty-&gt;display('page_container.tpl');<br/> ?&gt;</p> </td> </tr></tbody></table> 参见[display()](#)、[fetch()](#)、[{include}](#)和[{insert}](#)。 <table cellspacing="0" cellpadding="0" border="0" width="100%" summary="Footer navigation table"><tbody><tr><td valign="top" align="left" width="33%"><a accesskey="P" href="api.register.resource.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="api.unregisterfilter.html">Next</a></td> </tr><tr><td valign="top" align="left" width="33%">registerResource()<br/> 注册资源<br/></td> <td valign="top" align="center" width="34%"><a accesskey="U" href="api.functions.html">Up</a></td> <td valign="top" align="right" width="33%">unregisterFilter()<br/> 注销过滤器 </td> </tr></tbody></table>