<HTML ><HEAD ><TITLE >Custom 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="Smarty For Template Designers" HREF="smarty.for.designers.html"><LINK REL="PREVIOUS" TITLE="{strip}" HREF="language.function.strip.html"><LINK REL="NEXT" TITLE="{counter}" HREF="language.function.counter.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="chapter" 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="language.function.strip.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.function.counter.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.custom.functions" ></A >Chapter 8. Custom Functions</H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="language.custom.functions.html#language.function.assign" >{assign}</A ></DT ><DT ><A HREF="language.function.counter.html" >{counter}</A ></DT ><DT ><A HREF="language.function.cycle.html" >{cycle}</A ></DT ><DT ><A HREF="language.function.debug.html" >{debug}</A ></DT ><DT ><A HREF="language.function.eval.html" >{eval}</A ></DT ><DT ><A HREF="language.function.fetch.html" >{fetch}</A ></DT ><DT ><A HREF="language.function.html.checkboxes.html" >{html_checkboxes}</A ></DT ><DT ><A HREF="language.function.html.image.html" >{html_image}</A ></DT ><DT ><A HREF="language.function.html.options.html" >{html_options}</A ></DT ><DT ><A HREF="language.function.html.radios.html" >{html_radios}</A ></DT ><DT ><A HREF="language.function.html.select.date.html" >{html_select_date}</A ></DT ><DT ><A HREF="language.function.html.select.time.html" >{html_select_time}</A ></DT ><DT ><A HREF="language.function.html.table.html" >{html_table}</A ></DT ><DT ><A HREF="language.function.mailto.html" >{mailto}</A ></DT ><DT ><A HREF="language.function.math.html" >{math}</A ></DT ><DT ><A HREF="language.function.popup.html" >{popup}</A ></DT ><DT ><A HREF="language.function.popup.init.html" >{popup_init}</A ></DT ><DT ><A HREF="language.function.textformat.html" >{textformat}</A ></DT ></DL ></DIV ><P > Smarty comes with several custom functions that you can use in the templates. </P ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.function.assign" ></A >{assign}</H1 ><P > <TT CLASS="varname" >{assign}</TT > is used for assigning template variables <SPAN CLASS="bold" ><B CLASS="emphasis" >during the execution of a template</B ></SPAN >. </P ><DIV CLASS="informaltable" ><A NAME="AEN2671" ></A ><P ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><THEAD ><TR ><TH WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Attribute Name</TH ><TH WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Type</TH ><TH WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Required</TH ><TH WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Default</TH ><TH WIDTH="20%" ALIGN="LEFT" VALIGN="MIDDLE" >Description</TH ></TR ></THEAD ><TBODY ><TR ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >var</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >string</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Yes</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" ><SPAN CLASS="emphasis" ><I CLASS="emphasis" >n/a</I ></SPAN ></TD ><TD WIDTH="20%" ALIGN="LEFT" VALIGN="MIDDLE" >The name of the variable being assigned</TD ></TR ><TR ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >value</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >string</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Yes</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" ><SPAN CLASS="emphasis" ><I CLASS="emphasis" >n/a</I ></SPAN ></TD ><TD WIDTH="20%" ALIGN="LEFT" VALIGN="MIDDLE" >The value being assigned</TD ></TR ></TBODY ></TABLE ><P ></P ></DIV ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2700" ></A ><P ><B >Example 8-1. {assign}</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{assign var='name' value='Bob'} The value of $name is {$name}.</PRE ></TD ></TR ></TABLE ><P > The above example will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >The value of $name is Bob.</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2705" ></A ><P ><B >Example 8-2. {assign} with some maths</B ></P ><P >This complex example must have the variables in `backticks`</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{assign var=running_total value=`$running_total+$some_array[row].some_value`}</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2709" ></A ><P ><B >Example 8-3. Accessing {assign} variables from a PHP script</B ></P ><P > To access <TT CLASS="varname" >{assign}</TT > variables from a php script use <A HREF="api.get.template.vars.html" > <TT CLASS="varname" >get_template_vars()</TT ></A >. Here's the template that creates the variable <TT CLASS="parameter" ><I >$foo</I ></TT >. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{assign var='foo' value='Smarty'}</PRE ></TD ></TR ></TABLE ><P >The template variables are only available after/during template execution as in the following script. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// this will output nothing as the template has not been executed<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">get_template_vars</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// fetch the template to a variable<br /></span><span style="color: #0000BB">$whole_page </span><span style="color: #007700">= </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// this will output 'smarty' as the template has been executed<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">get_template_vars</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<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: #DD0000">'foo'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Even smarter'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// this will output 'Even smarter'<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">get_template_vars</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > The following functions can also <SPAN CLASS="emphasis" ><I CLASS="emphasis" >optionally</I ></SPAN > assign template variables. </P ><P > <A HREF="language.builtin.functions.html#language.function.capture" ><TT CLASS="varname" >{capture}</TT ></A >, <A HREF="language.function.include.html" ><TT CLASS="varname" >{include}</TT ></A >, <A HREF="language.function.include.php.html" ><TT CLASS="varname" >{include_php}</TT ></A >, <A HREF="language.function.insert.html" ><TT CLASS="varname" >{insert}</TT ></A >, <A HREF="language.function.counter.html" ><TT CLASS="varname" >{counter}</TT ></A >, <A HREF="language.function.cycle.html" ><TT CLASS="varname" >{cycle}</TT ></A >, <A HREF="language.function.eval.html" ><TT CLASS="varname" >{eval}</TT ></A >, <A HREF="language.function.fetch.html" ><TT CLASS="varname" >{fetch}</TT ></A >, <A HREF="language.function.math.html" ><TT CLASS="varname" >{math}</TT ></A >, <A HREF="language.function.textformat.html" ><TT CLASS="varname" >{textformat}</TT ></A > </P ><P > See also <A HREF="api.assign.html" ><TT CLASS="varname" >assign()</TT ></A > and <A HREF="api.get.template.vars.html" ><TT CLASS="varname" >get_template_vars()</TT ></A >. </P ></DIV ></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="language.function.strip.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="language.function.counter.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >{strip}</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="smarty.for.designers.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >{counter}</TD ></TR ></TABLE ></DIV ></BODY ></HTML >