<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.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.block.functions.html" accesskey="N">Next</a></td></tr></table> # Modifiers[调节器] Modifiers are little functions that are applied to a variable in the template before it is displayed or used in some other context. Modifiers can be chained together. 调节器是一些简短的函数,这些函数被应用于模板显示前的一个变量,或者其它的一些语境。调节器可以串联起来(执行)。 > mixed smarty_modifier_name($value, $param1); mixed $value; [mixed $param1, ...]; The first parameter to the modifier plugin is the value on which the modifier is to operate. The rest of the parameters are optional, depending on what kind of operation is to be performed. The modifier has to return [http://php.net/return] the result of its processing. 调节器插件的第一个参数应该直接了当地声明处理什么类型(可以是字符串、数组、对象等等这些类型)。其它的参数是可选的,取决于执行的操作类型。 调节器必须返回处理结果。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN4163" id="AEN4163"/><b>Example 16.3. simple modifier plugin</b><br/><strong>例16-3 .简单的调节器插件</strong><br/>This plugin basically aliases one of the built-in PHP functions. It does not have any additional parameters.<br/> 本插件从本质上说php内置函数之一的别名。他没有任何多余的参数。 <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">&lt;?php/* * Smarty plugin * ------------------------------------------------------------- * File: modifier.capitalize.php * Type: modifier * Name: capitalize * Purpose: capitalize words in the string * ------------------------------------------------------------- */function smarty_modifier_capitalize($string){ return ucwords($string);}?&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.4. More complex modifier plugin<br/></b><strong>例16-4. 复杂一些的调节器插件</strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">&lt;?php/* * Smarty plugin * ------------------------------------------------------------- * File: modifier.truncate.php * Type: modifier * Name: truncate * Purpose: Truncate a string to a certain length if necessary, * optionally splitting in the middle of a word, and * appending the $etc string. * ------------------------------------------------------------- */function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false){ if ($length == 0) return ''; if (strlen($string) &gt; $length) { $length -= strlen($etc); $fragment = substr($string, 0, $length+1); if ($break_words) $fragment = substr($fragment, 0, -1); else $fragment = preg_replace('/\s+(\S+)?$/', '', $fragment); return $fragment.$etc; } else return $string;}?&gt;</pre></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.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.block.functions.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Template 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">Block Functions<br/> 块函数</td></tr></table>