Variables
Smarty has several different types of variables. The type of the variable
depends on what symbol it is prefixed with (or enclosed within).
Variables in Smarty can be either displayed directly or used as arguments
for function attributes and modifiers, inside conditional expressions,
etc. To print a variable, simply enclose it in the delimiters so that it
is the only thing contained between them. Examples:
]]>
Variables assigned from PHP
Variables that are assigned from PHP are referenced by preceding them with
a dollar sign $. Variables assigned from within the
template with the assign
function are also displayed this way.
assigned variables
Your last login was on {$lastLoginDate}.
]]>
This will output:
Your last login was on January 11th, 2001.
]]>
Associative arrays
You can also reference associative array variables that are
assigned from PHP by specifying the key after the '.' (period)
symbol.
accessing associative array variables
assign('Contacts',
array('fax' => '555-222-9876',
'email' => 'zaphod@slartibartfast.com',
'phone' => array('home' => '555-444-3333',
'cell' => '555-111-1234')));
$smarty->display('index.tpl');
?>
]]>
where the content of index.tpl is:
{$Contacts.email}
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}
{$Contacts.phone.cell}
]]>
this will output:
zaphod@slartibartfast.com
555-444-3333
555-111-1234
]]>
Array indexes
You can reference arrays by their index, much like native PHP
syntax.
accessing arrays by index
assign('Contacts',
array('555-222-9876',
'zaphod@slartibartfast.com',
array('555-444-3333',
'555-111-1234')));
$smarty->display('index.tpl');
?>
]]>
where index.tpl is:
{$Contacts[1]}
{* you can print arrays of arrays as well *}
{$Contacts[2][0]}
{$Contacts[2][1]}
]]>
This will output:
zaphod@slartibartfast.com
555-444-3333
555-111-1234
]]>
Objects
Properties of objects assigned from PHP can be referenced
by specifying the property name after the '->' symbol.
accessing object properties
email: {$person->email}
]]>
this will output:
email: zaphod@slartibartfast.com
]]>
Variables loaded from config files
Variables that are loaded from the config files are referenced by
enclosing them within hash marks (#), or with the smarty variable
$smarty.config.
The second syntax is useful for embedding into quoted attribute
values.
config variables
foo.conf:
index.tpl:
{#pageTitle#}