<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Chapter 8. Custom 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="smarty.for.designers.html" title="Part II. Smarty For Template Designers"> <link rel="prev" href="language.function.strip.html" title="{strip}"> <link rel="next" href="language.function.counter.html" title="{counter}"> </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">Chapter 8. Custom Functions</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="language.function.strip.html">Prev</a> </td> <th width="60%" align="center">Part II. Smarty For Template Designers</th> <td width="20%" align="right"> <a accesskey="n" href="language.function.counter.html">Next</a> </td> </tr> </table> <hr> </div> <div class="chapter" title="Chapter 8. Custom Functions"> <div class="titlepage"><div><div><h2 class="title"> <a name="language.custom.functions"></a>Chapter 8. Custom Functions</h2></div></div></div> <div class="toc"> <p><b>Table of Contents</b></p> <dl> <dt><span class="sect1"><a href="language.custom.functions.html#language.function.assign">{assign}</a></span></dt> <dt><span class="sect1"><a href="language.function.counter.html">{counter}</a></span></dt> <dt><span class="sect1"><a href="language.function.cycle.html">{cycle}</a></span></dt> <dt><span class="sect1"><a href="language.function.debug.html">{debug}</a></span></dt> <dt><span class="sect1"><a href="language.function.eval.html">{eval}</a></span></dt> <dt><span class="sect1"><a href="language.function.fetch.html">{fetch}</a></span></dt> <dt><span class="sect1"><a href="language.function.html.checkboxes.html">{html_checkboxes}</a></span></dt> <dt><span class="sect1"><a href="language.function.html.image.html">{html_image}</a></span></dt> <dt><span class="sect1"><a href="language.function.html.options.html">{html_options}</a></span></dt> <dt><span class="sect1"><a href="language.function.html.radios.html">{html_radios}</a></span></dt> <dt><span class="sect1"><a href="language.function.html.select.date.html">{html_select_date}</a></span></dt> <dt><span class="sect1"><a href="language.function.html.select.time.html">{html_select_time}</a></span></dt> <dt><span class="sect1"><a href="language.function.html.table.html">{html_table}</a></span></dt> <dt><span class="sect1"><a href="language.function.mailto.html">{mailto}</a></span></dt> <dt><span class="sect1"><a href="language.function.math.html">{math}</a></span></dt> <dt><span class="sect1"><a href="language.function.textformat.html">{textformat}</a></span></dt> </dl> </div> <p> Smarty comes with several custom functions that you can use in the templates. </p> <div class="sect1" title="{assign}"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="language.function.assign"></a>{assign}</h2></div></div></div> <p> <code class="varname">{assign}</code> is used for assigning template variables <span class="bold"><strong>during the execution of a template</strong></span>. </p> <div class="informaltable"><table border="1"> <colgroup> <col align="center"> <col align="center"> <col align="center"> <col align="center"> <col> </colgroup> <thead><tr> <th align="center">Attribute Name</th> <th align="center">Type</th> <th align="center">Required</th> <th align="center">Default</th> <th>Description</th> </tr></thead> <tbody> <tr> <td align="center">var</td> <td align="center">string</td> <td align="center">Yes</td> <td align="center"><span class="emphasis"><em>n/a</em></span></td> <td>The name of the variable being assigned</td> </tr> <tr> <td align="center">value</td> <td align="center">string</td> <td align="center">Yes</td> <td align="center"><span class="emphasis"><em>n/a</em></span></td> <td>The value being assigned</td> </tr> </tbody> </table></div> <div class="example"> <a name="id3064116"></a><p class="title"><b>Example 8.1. {assign}</b></p> <div class="example-contents"> <pre class="programlisting"> {assign var='name' value='Bob'} The value of $name is {$name}. </pre> <p> The above example will output: </p> <pre class="screen"> The value of $name is Bob. </pre> </div> </div> <br class="example-break"><div class="example"> <a name="id3064143"></a><p class="title"><b>Example 8.2. {assign} with some maths</b></p> <div class="example-contents"> <p>This complex example must have the variables in `backticks`</p> <pre class="programlisting"> {assign var=running_total value=`$running_total+$some_array[row].some_value`} </pre> </div> </div> <br class="example-break"><div class="example"> <a name="id3064163"></a><p class="title"><b>Example 8.3. Accessing {assign} variables from a PHP script</b></p> <div class="example-contents"> <p> To access <code class="varname">{assign}</code> variables from a php script use <a class="link" href="api.get.template.vars.html" title="get_template_vars()"> <code class="varname">get_template_vars()</code></a>. Here's the template that creates the variable <em class="parameter"><code>$foo</code></em>. </p> <pre class="programlisting"> {assign var='foo' value='Smarty'} </pre> <p>The template variables are only available after/during template execution as in the following script. </p> <pre class="programlisting"> <?php // this will output nothing as the template has not been executed echo $smarty->get_template_vars('foo'); // fetch the template to a variable $whole_page = $smarty->fetch('index.tpl'); // this will output 'smarty' as the template has been executed echo $smarty->get_template_vars('foo'); $smarty->assign('foo','Even smarter'); // this will output 'Even smarter' echo $smarty->get_template_vars('foo'); ?> </pre> </div> </div> <br class="example-break"><p> The following functions can also <span class="emphasis"><em>optionally</em></span> assign template variables. </p> <p> <a class="link" href="language.builtin.functions.html#language.function.capture" title="{capture}"><code class="varname">{capture}</code></a>, <a class="link" href="language.function.include.html" title="{include}"><code class="varname">{include}</code></a>, <a class="link" href="language.function.include.php.html" title="{include_php}"><code class="varname">{include_php}</code></a>, <a class="link" href="language.function.insert.html" title="{insert}"><code class="varname">{insert}</code></a>, <a class="link" href="language.function.counter.html" title="{counter}"><code class="varname">{counter}</code></a>, <a class="link" href="language.function.cycle.html" title="{cycle}"><code class="varname">{cycle}</code></a>, <a class="link" href="language.function.eval.html" title="{eval}"><code class="varname">{eval}</code></a>, <a class="link" href="language.function.fetch.html" title="{fetch}"><code class="varname">{fetch}</code></a>, <a class="link" href="language.function.math.html" title="{math}"><code class="varname">{math}</code></a>, <a class="link" href="language.function.textformat.html" title="{textformat}"><code class="varname">{textformat}</code></a> </p> <p> See also <a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a> and <a class="link" href="api.get.template.vars.html" title="get_template_vars()"><code class="varname">get_template_vars()</code></a>. </p> </div> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="language.function.strip.html">Prev</a> </td> <td width="20%" align="center"><a accesskey="u" href="smarty.for.designers.html">Up</a></td> <td width="40%" align="right"> <a accesskey="n" href="language.function.counter.html">Next</a> </td> </tr> <tr> <td width="40%" align="left" valign="top">{strip} </td> <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td> <td width="40%" align="right" valign="top"> {counter}</td> </tr> </table> </div> </body> </html>