<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.compiler.functions.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 16. Extending Smarty With Plugins<br/></td><td width="25%" align="right" valign="bottom"><a href="plugins.outputfilters.html" accesskey="N">Next</a></td></tr></table> # Prefilters/Postfilters[预滤器/后滤器] Prefilter and postfilter plugins are very similar in concept; where they differ is in the execution -- more precisely the time of their execution. 从概念上看,预滤器和后滤器插件很相似;不同之处就在于它们的执行,更确切地说是它们的执行时刻。 > string smarty_prefilter_name($source, &$smarty); > string $source; > object &$smarty; Prefilters are used to process the source of the template immediately before compilation. The first parameter to the prefilter function is the template source, possibly modified by some other prefilters. The plugin is supposed to return the modified source. Note that this source is not saved anywhere, it is only used for compilation. 预滤器用来在编译之前直接处理模板资源(源文件)。预滤器函数的第一个参数是模板资源,该文件可能被其他预过滤器处理过。此插件将返回修改过的资源。请记住此资源仅用来编译,它不会在任何地方被保存。 > string smarty_postfilter_name($compiled, &$smarty); string $compiled; object &$smarty; Postfilters are used to process the compiled output of the template (the PHP code) immediately after the compilation is done but before the compiled template is saved to the filesystem. The first parameter to the postfilter function is the compiled template code, possibly modified by other postfilters. The plugin is supposed to return the modified version of this code. 后过滤器用在编译完成后,编译模板保存到文件系统前,立即处理模板的编译输出(PHP代码)。预过滤器函数的第一个参数是编译之后的模板代码,该代码可能被其他后过滤器处理过。后滤器插件将返回处理过的php代码译本。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 译注 </caption> <tr><td>模板、预过滤器、编译、后过滤器、保存编译的关系应该是这样的:<br/><img src="https://box.kancloud.cn/2015-09-02_55e669a1b872f.gif"/></td> </tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN4167" id="AEN4167"/><b>Example 16.7. prefilter plugin<br/>例16-7.预滤器插件</b><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">&lt;?php/* * Smarty plugin * ------------------------------------------------------------- * File: prefilter.pre01.php * Type: prefilter * Name: pre01 * Purpose: Convert html tags to be lowercase. * ------------------------------------------------------------- */ function smarty_prefilter_pre01($source, $smarty) { return preg_replace('!&lt;(\w+)[^&gt;]+&gt;!e', 'strtolower("$1")', $source); }?&gt;</pre></td></tr></table></div></td></tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <p><b>Example 16.8. postfilter plugin<br/>例16-8.后滤器插件</b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">&lt;?php/* * Smarty plugin * ------------------------------------------------------------- * File: postfilter.post01.php * Type: postfilter * Name: post01 * Purpose: Output code that lists all current template vars. * ------------------------------------------------------------- */ function smarty_postfilter_post01($compiled, $smarty) { $compiled = "&lt;pre&gt;\n&lt;?php print_r(\$this-&gt;getTemplateVars()); ?&gt;\n&lt;/pre&gt;" . $compiled; return $compiled; }?&gt;</pre></td></tr></table><p> 参见<a href="api.registerfilter.html">registerFilter()</a>和<a href="api.unregisterfilter.html">unregisterFilter()</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.compiler.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.outputfilters.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Compiler 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">Output Filters<br/> 输出过滤器</td></tr></table>