<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.registerfilter.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.register.object.html">Next</a></td> </tr></tbody></table> # registerPlugin()[注册插件] Name registerPlugin() ― dynamically register plugins 动态注册插件 Description void registerPlugin(string type, string name, mixed callback, bool cacheable, mixed cache_attrs); This method registers functions or methods defined in your script as plugin. It uses the following parameters: type defines the type of the plugin. Valid values are "function", "block", "compiler" and "modifier". name defines the name of the plugin. 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. cacheable and cache_attrs can be omitted in most cases. See controlling cacheability of plugins output on how to use them properly. 本方法在脚本中注册函数或方法作为插件。其参数如下: “type”定义插件的类型,其值为下列之一:“function”、“block”、“compiler”和“modifier”; “name”定义插件的函数名; “callback”为定义的php回调函数,其类型为下列之一: 1、包含函数名的字符串; 2、格式为(&$object, $method)的数组,其中,&$object为引用对象,$method为包含方法名的字符串; 3、格式为($class, $method)的数组,其中,$class为类名,$method为类中的方法。 “cacheable”和“cache_attrs”参数大多情况下可以省略。参见[如何正确地控制插件缓存输出](#)。 <table class="EXAMPLE" cellspacing="0" cellpadding="0" border="0" width="100%"><tbody><tr><td> <div class="EXAMPLE"><strong><a name="AEN31327" id="AEN31327"/>Example 13.27. register a function plugin</strong><b><br/>例13-27.注册一个<b>函数</b>插件<br/></b><table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td> <pre class="PROGRAMLISTING">&lt;?php$smarty-&gt;registerPlugin("function","date_now", "print_current_date");function print_current_date($params, $smarty){ if(empty($params["format"])) { $format = "%b %e, %Y"; } else { $format = $params["format"]; } return strftime($format,time());}?&gt; And in the template{date_now}{* or to format differently *} {* 或者运用自己的格式化形式 *}{date_now format="%Y/%m/%d"}</pre></td> </tr></tbody></table><p><span class="PROGRAMLISTING"><strong>Example 13.28. register block function plugin</strong></span><b><br/>例13-28.注册一个<b>块函数</b>插件</b></p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="PROGRAMLISTING">&lt;?php// function declarationfunction do_translation ($params, $content, $smarty, &amp;$repeat, $template){ if (isset($content)) { $lang = $params["lang"]; // do some translation with $content return $translation; }}// register with smarty$smarty-&gt;registerPlugin("block","translate", "do_translation");?&gt; Where the template is:{translate lang="br"}Hello, world!{/translate}</pre></td> </tr></tbody></table><p><strong>Example 13.29. register modifier plugin</strong><b><br/>例13-29.注册一个<b>调节器</b>插件</b></p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td>&lt;?php<br/> // let's map PHP's stripslashes function to a Smarty modifier. 让我们将php的stripslashes()函数映射到Smarty调节器中 <br/> $smarty-&gt;registerPlugin("modifier","ss", "stripslashes");<br/> ?&gt;<br/> In the template, use ss to strip slashes.<br/> &lt;?php<br/> {$var|ss}<br/> ?&gt;</td> </tr></tbody></table><p> 参见<a href="api.unregisterplugin.html">unregisterPlugin()</a>、<a href="plugins.writing.html">插件函数</a>、<a href="plugins.block.functions.html">插件块函数</a>、<a href="plugins.compiler.functions.html">插件编译函数</a>和<a href="plugins.modifiers.html">建立调节器插件</a>一节。</p> </div> </td> </tr></tbody></table> <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.registerfilter.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.register.object.html">Next</a></td> </tr><tr><td valign="top" align="left" width="33%">registerFilter()<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%">registerObject()<br/> 注册对象</td> </tr></tbody></table>