<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><th colspan="3" align="center">Smarty - the compiling PHP template engine</th></tr><tr><td width="25%" align="left" valign="bottom"><a href="plugins.block.functions.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 16. Extending Smarty With Plugins</td><td width="25%" align="right" valign="bottom"><a href="plugins.prefilters.postfilters.html" accesskey="N">Next</a></td></tr></table> # Compiler Functions[编译函数] Compiler functions are called only during compilation of the template. They are useful for injecting PHP code or time-sensitive static content into the template. If there is both a compiler function and a custom function registered under the same name, the compiler function has precedence. 编译函数仅在模板编译期间被调用。编译函数对于将PHP代码或对时间敏感的静态内容注入到模板时非常有用。如果同时注册同名的编译函数和自定义函数,则优先使用编译函数。 mixed smarty_compiler_name($tag_arg, &$smarty); string $tag_arg; object &$smarty; The compiler function is passed two parameters: the tag argument string - basically, everything from the function name until the ending delimiter, and the Smarty object. It's supposed to return the PHP code to be injected into the compiled template. 编译函数有两个参数:参数string标签――基本上包含从函数名称到结束定界符的所有内容,另一个参数是Smarty对象。该函数将返回嵌入到编译模板中的PHP代码。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN4044"/><b>Example 16.6. simple compiler function<br/>例16-6.简单编译函数</b><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <p>&lt;?php<br/> /*<br/> * Smarty plugin<br/> * -------------------------------------------------------------<br/> * File: compiler.tplheader.php<br/> * Type: compiler<br/> * Name: tplheader<br/> * Purpose: Output header containing the source file name and<br/> * the time it was compiled.<br/> * -------------------------------------------------------------<br/> */<br/> function smarty_compiler_tplheader($tag_arg, $smarty)<br/> {<br/> return "\necho '" . $smarty-&gt;_current_file . " compiled at " . date('Y-m-d H:M'). "';";<br/> }<br/> ?&gt;</p> <p>This function can be called from the template as: 在模板中这样调用编译函数</p> <p>{* this function gets executed at compile time only *}<br/> {* 本函数仅在编译运行时执行 *} <br/> {tplheader}<br/><br/> The resulting PHP code in the compiled template would be something like this: 返回给编译模板的php代码</p> <p>&lt;?php<br/> echo 'index.tpl compiled at 2002-02-20 20:02';<br/> ?&gt;</p> </td></tr></table><p> 参见<a href="api.registerplugin.html">registerPlugin()</a>、<a href="api.unregisterplugin.html">unregisterPlugin()</a>。</p></div></td></tr></table> <table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="plugins.block.functions.html" accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="plugins.prefilters.postfilters.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Block Functions<br/> 块函数</td><td width="34%" align="center" valign="top"><a href="plugins.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">Prefilters/Postfilters<br/> 预滤器/后滤器</td></tr></table>