PHP Manual

PEAR Manual

Smarty Manual

PostgreSQL

MySQL Manual

Perl Manual

chapter 8. custom functions

smarty comes with several custom functions that you can use in the templates.

{assign}

{assign} is used for assigning template variables during the execution of the template.

attribute nametyperequireddefaultdescription
varstringyesn/athe name of the variable being assigned
valuestringyesn/athe value being assigned

example 8-1. {assign}

{assign var="name" value="bob"}  the value of $name is {$name}.

the above example will output:

the value of $name is bob.

example 8-2. accessing {assign} variables from a php script.

to access {assign} variables from php use get_template_vars(). however, the variables are only available after/during template execution as in the following example

{* index.tpl *} {assign var="foo" value="smarty"}
<?php

// this will output nothing as the template has not been executed
echo $smarty->get_template_vars('foo');

// fetch the template to a dead variable
$dead $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');

?>

the following functions can also optionally assign template variables.

{capture}, {include}, {include_php}, {insert}, {counter}, {cycle}, {eval}, {fetch}, {math}, {textformat}

see also assign() and get_template_vars().