<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.writing.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.modifiers.html" accesskey="N">Next</a></td></tr></table> # Template Functions[模板函数] > void smarty_function_name($params, &$smarty); array $params; object &$smarty; All attributes passed to template functions from the template are contained in the $params as an associative array. The output (return value) of the function will be substituted in place of the function tag in the template, eg the {fetch} function. Alternatively, the function can simply perform some other task without any output, eg the {assign} function. If the function needs to assign some variables to the template or use some other Smarty-provided functionality, it can use the supplied $smarty object to do so eg $smarty->foo(). 模板传递给模板函数的所有属性都包含在关联数组$params中。 在模板中,函数的输出内容(返回值)在原位置用函数标签代替,例如{fetch}函数。作为另一种选择,函数也可以单纯地用来做些非输出内容的任务,如{assign}函数。 如果函数需要分配(俗话说的赋值)一些变量给模板或者使用Smarty提供的一些函数,可以通过$smarty对象实现,如$smarty->foo()。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN4161" id="AEN4161"/><b>Example 16.1. function plugin with output</b><br/><strong>例16-1.带输出的函数插件</strong><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">&lt;?php/* * Smarty plugin * ------------------------------------------------------------- * File: function.eightball.php * Type: function * Name: eightball * Purpose: outputs a random magic answer * ------------------------------------------------------------- */function smarty_function_eightball($params, $smarty){ $answers = array('Yes', 'No', 'No way', 'Outlook not so good', 'Ask again soon', 'Maybe in your reality'); $result = array_rand($answers); return $answers[$result];}?&gt;which can be used in the template as:Question: Will we ever have time travel?Answer: {eightball}. </pre></td></tr></table></div></td></tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><b>Example 16.2. function plugin without output</b><br/><strong>例16-2.不带输出的函数</strong><strong>插件</strong><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td>&lt;?php<br/> /*<br/>* Smarty plugin<br/>* -------------------------------------------------------------<br/>* File: function.assign.php<br/>* Type: function<br/>* Name: assign<br/>* Purpose: assign a value to a template variable<br/>* -------------------------------------------------------------<br/>*/<br/>function smarty_function_assign($params, $smarty)<br/>{<br/>if (empty($params['var'])) {<br/>$smarty-&gt;triggerError("assign: missing 'var' parameter");<br/>return;<br/>}<br/>if (!in_array('value', array_keys($params))) {<br/>$smarty-&gt;triggerError("assign: missing 'value' parameter");<br/>return;<br/>}<br/>$smarty-&gt;assign($params['var'], $params['value']);<br/>}<br/>?&gt;</td></tr></table><p> 参见<a href="api.register.function.html">registerPlugin()</a>、<a href="api.unregister.function.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.writing.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.modifiers.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Writing Plugins<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">Modifiers<br/> 调节器插件</td></tr></table>