Variables assigned from PHP
Variables that are
assigned from PHP are referenced by
preceding them with a dollar sign $ (like php).
Variables assigned from within a template with the
{assign}
function are also displayed this way.
Assigned variables php script
assign('firstname', 'Doug');
$smarty->assign('lastname', 'Evans');
$smarty->assign('meetingPlace', 'New York');
$smarty->display('index.tpl');
?>
]]>
where the content of index.tpl is:
{* this will not work as $variables are case sensitive *}
This weeks meeting is in {$meetingplace}.
{* this will work *}
This weeks meeting is in {$meetingPlace}.
]]>
This above would output:
This weeks meeting is in .
This weeks meeting is in New York.
]]>
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.example.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.example.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.example.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.example.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
name}
email: {$person->email}
]]>
this will output:
email: zaphod@slartibartfast.example.com
]]>