<HTML ><HEAD ><TITLE >Template Functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Smarty Manual" HREF="index.html"><LINK REL="UP" TITLE="Extending Smarty With Plugins" HREF="plugins.html"><LINK REL="PREVIOUS" TITLE="Writing Plugins" HREF="plugins.writing.html"><LINK REL="NEXT" TITLE="Modifiers" HREF="plugins.modifiers.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Smarty Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="plugins.writing.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 16. Extending Smarty With Plugins</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="plugins.modifiers.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="plugins.functions" ></A >Template Functions</H1 ><DIV CLASS="funcsynopsis" ><A NAME="AEN7191" ></A ><P ></P ><P ><CODE ><CODE CLASS="FUNCDEF" >void <B CLASS="function" >smarty_function_<TT CLASS="replaceable" ><I >name</I ></TT ></B ></CODE > (array $params, object &$smarty)</CODE ></P ><P ></P ></DIV ><P > All <A HREF="language.syntax.attributes.html" >attributes</A > passed to template functions from the template are contained in the <TT CLASS="parameter" ><I >$params</I ></TT > 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 HREF="language.function.fetch.html" ><TT CLASS="varname" >{fetch}</TT ></A > function. Alternatively, the function can simply perform some other task without any output, eg the <A HREF="language.custom.functions.html#language.function.assign" > <TT CLASS="varname" >{assign}</TT ></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 <TT CLASS="parameter" ><I >$smarty</I ></TT > object to do so eg <TT CLASS="literal" >$smarty->foo()</TT >. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7212" ></A ><P ><B >Example 16-1. function plugin with output</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/*<br /> * Smarty plugin<br /> * -------------------------------------------------------------<br /> * File: function.eightball.php<br /> * Type: function<br /> * Name: eightball<br /> * Purpose: outputs a random magic answer<br /> * -------------------------------------------------------------<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">smarty_function_eightball</span><span style="color: #007700">(</span><span style="color: #0000BB">$params</span><span style="color: #007700">, &</span><span style="color: #0000BB">$smarty</span><span style="color: #007700">)<br />{<br /> </span><span style="color: #0000BB">$answers </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'Yes'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'No'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'No way'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'Outlook not so good'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'Ask again soon'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'Maybe in your reality'</span><span style="color: #007700">);<br /><br /> </span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">array_rand</span><span style="color: #007700">(</span><span style="color: #0000BB">$answers</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">$answers</span><span style="color: #007700">[</span><span style="color: #0000BB">$result</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > which can be used in the template as: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >Question: Will we ever have time travel? Answer: {eightball}.</PRE ></TD ></TR ></TABLE ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7218" ></A ><P ><B >Example 16-2. function plugin without output</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/*<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 /></span><span style="color: #007700">function </span><span style="color: #0000BB">smarty_function_assign</span><span style="color: #007700">(</span><span style="color: #0000BB">$params</span><span style="color: #007700">, &</span><span style="color: #0000BB">$smarty</span><span style="color: #007700">)<br />{<br /> if (empty(</span><span style="color: #0000BB">$params</span><span style="color: #007700">[</span><span style="color: #DD0000">'var'</span><span style="color: #007700">])) {<br /> </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"assign: missing 'var' parameter"</span><span style="color: #007700">);<br /> return;<br /> }<br /><br /> if (!</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #DD0000">'value'</span><span style="color: #007700">, </span><span style="color: #0000BB">array_keys</span><span style="color: #007700">(</span><span style="color: #0000BB">$params</span><span style="color: #007700">))) {<br /> </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"assign: missing 'value' parameter"</span><span style="color: #007700">);<br /> return;<br /> }<br /><br /> </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #0000BB">$params</span><span style="color: #007700">[</span><span style="color: #DD0000">'var'</span><span style="color: #007700">], </span><span style="color: #0000BB">$params</span><span style="color: #007700">[</span><span style="color: #DD0000">'value'</span><span style="color: #007700">]);<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also: <A HREF="api.register.function.html" ><TT CLASS="varname" >register_function()</TT ></A >, <A HREF="api.unregister.function.html" ><TT CLASS="varname" >unregister_function()</TT ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><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</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</TD ></TR ></TABLE ></DIV ></BODY ></HTML >