<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Template Functions</title> <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> <link rel="home" href="index.html" title="Smarty Manual"> <link rel="up" href="plugins.html" title="Chapter 16. Extending Smarty With Plugins"> <link rel="prev" href="plugins.writing.html" title="Writing Plugins"> <link rel="next" href="plugins.modifiers.html" title="Modifiers"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">Template Functions</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="plugins.writing.html">Prev</a> </td> <th width="60%" align="center">Chapter 16. Extending Smarty With Plugins</th> <td width="20%" align="right"> <a accesskey="n" href="plugins.modifiers.html">Next</a> </td> </tr> </table> <hr> </div> <div class="sect1" title="Template Functions"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="plugins.functions"></a>Template Functions</h2></div></div></div> <div class="funcsynopsis"> <table border="0" summary="Function synopsis" cellspacing="0" cellpadding="0" class="funcprototype-table"> <tr> <td><code class="funcdef">void <b class="fsfunc">smarty_function_name</b>(</code></td> <td> <var class="pdparam">$params</var>, </td> <td> </td> </tr> <tr> <td> </td> <td> <var class="pdparam">&$smarty</var><code>)</code>;</td> <td> </td> </tr> </table> <div class="paramdef-list"> <code>array <var class="pdparam">$params</var></code>;<br><code>object <var class="pdparam">&$smarty</var></code>;</div> <div class="funcprototype-spacer"> </div> </div> <p> All <a class="link" href="language.syntax.attributes.html" title="Attributes">attributes</a> passed to template functions from the template are contained in the <em class="parameter"><code>$params</code></em> as an associative array. </p> <p> The output (return value) of the function will be substituted in place of the function tag in the template, eg the <a class="link" href="language.function.fetch.html" title="{fetch}"><code class="varname">{fetch}</code></a> function. Alternatively, the function can simply perform some other task without any output, eg the <a class="link" href="language.custom.functions.html#language.function.assign" title="{assign}"> <code class="varname">{assign}</code></a> function. </p> <p> If the function needs to assign some variables to the template or use some other Smarty-provided functionality, it can use the supplied <em class="parameter"><code>$smarty</code></em> object to do so eg <code class="literal">$smarty->foo()</code>. </p> <p> </p> <div class="example"> <a name="id3125404"></a><p class="title"><b>Example 16.1. function plugin with output</b></p> <div class="example-contents"><pre class="programlisting"> <?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]; } ?> </pre></div> </div> <p><br class="example-break"> </p> <p> which can be used in the template as: </p> <pre class="programlisting"> Question: Will we ever have time travel? Answer: {eightball}. </pre> <p> </p> <div class="example"> <a name="id3125455"></a><p class="title"><b>Example 16.2. function plugin without output</b></p> <div class="example-contents"><pre class="programlisting"> <?php /* * Smarty plugin * ------------------------------------------------------------- * File: function.assign.php * Type: function * Name: assign * Purpose: assign a value to a template variable * ------------------------------------------------------------- */ function smarty_function_assign($params, &$smarty) { if (empty($params['var'])) { $smarty->trigger_error("assign: missing 'var' parameter"); return; } if (!in_array('value', array_keys($params))) { $smarty->trigger_error("assign: missing 'value' parameter"); return; } $smarty->assign($params['var'], $params['value']); } ?> </pre></div> </div> <p><br class="example-break"> </p> <p> See also: <a class="link" href="api.register.function.html" title="register_function()"><code class="varname">register_function()</code></a>, <a class="link" href="api.unregister.function.html" title="unregister_function"><code class="varname">unregister_function()</code></a>. </p> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="plugins.writing.html">Prev</a> </td> <td width="20%" align="center"><a accesskey="u" href="plugins.html">Up</a></td> <td width="40%" align="right"> <a accesskey="n" href="plugins.modifiers.html">Next</a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Writing Plugins </td> <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td> <td width="40%" align="right" valign="top"> Modifiers</td> </tr> </table> </div> </body> </html>