<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.load.filter.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.registerplugin.html">Next</a></td> </tr></tbody></table> # registerFilter()[注册过滤器] Name registerFilter() ― dynamically register filters 动态注册过滤器 Description void registerFilter(string type, mixed callback); Use this to dynamically register filters to operate on a templates. It uses the following parameters: type defines the type of the filter. Valid values are "pre", "post", "output" and "variable". callback defines the PHP callback. it can be either: 1.A string containing the function name 2.An array of the form array(&$object, $method) with &$object being a reference to an object and $method being a string containing the method-name 3.An array of the form array($class, $method) with $class being the class name and $method being a method of the class. 本函数在模板中动态地注册过滤器。其参数如下: “type”定义过滤器的类型,其值为下列之一:“pre”、“post”、“output”和“variable”; “callback”为定义的php回调函数,其类型为下列之一: 1、包含函数名的字符串; 2、格式为(&$object, $method)的数组,其中,&$object为引用对象,$method为包含方法名的字符串; 3、格式为($class, $method)的数组,其中,$class为类名,$method为类中的方法。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 技术提示 </caption> <tr><td>Technical Note<br/> If the chosen function callback is of the form array(&amp;$object, $method), only one instance of the same class and with the same $method can be registered. The latest registered function callback will be used in such a scenario. <br/> 如果选择array(&amp;$object, $method)格式的回调函数,带相同方法($method)的相同类,只能注册一个实例。在这种情况下将使用最后注册的回调函数。</td> </tr></table> 译注 | 预过滤器(Prefilters)、后过滤器(Postfilters)和输出过滤器(Output Fiters)都可以使用本方法注册。 | |-----| A prefilter runs through the template source before it gets compiled. See template prefilters for more information on how to setup a prefiltering function. A postfilter runs through the template code after it was compiled to PHP. See template postfilters for more information on how to setup a postfiltering function. A outputfilter operates on a template's output before it is displayed. See template output filters for more information on how to set up an output filter function. See also unregisterFilter(), loadFilter(), $autoload_filters, template pre filters template post filters template output filters section. 预过滤器在模板资源被编译之前运行。查看[模板预过滤器](#)获取更多关于怎么设置预过滤器函数的信息。 发送过滤器在模板被编译为php之后运行。查看[模板发送过滤器](#)获取更多关于怎么设置发送过滤器函数的信息。 输出过滤器在模板输出未显示之前运行。查看[模板输出过滤器](#)获取更多关于怎么设置输出过滤器函数的信息。 参见[unregisterFilter()](#)、[loadFilter()](#)、[$autoload_filters](#)、[模板预过滤器](#)、[模板发送过滤器](#)、[模板输出过滤器](#)章节。 <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.load.filter.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.registerplugin.html">Next</a></td> </tr><tr><td valign="top" align="left" width="33%">loadFilter()<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%">registerPlugin()<br/> 注册插件</td> </tr></tbody></table>