Variables

Chapter 4. Variables

Smarty has several different types of variables. The type of the variable depends on what symbol it is prefixed 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:
{$Name}

{$Contacts[row].Phone}

<body bgcolor="{#bgcolor#}">

Variables assigned from PHP

Variables that are assigned from PHP are referenced by preceding them with a dollar sign $. Variables assigned from within a template with the {assign} function are also displayed this way.

Example 4-1. assigned variables

php script

<?php

$smarty 
= new Smarty;

$smarty->assign('firstname''Doug');
$smarty->assign('lastname''Evans');
$smarty->assign('meetingPlace''New York');

$smarty->display('index.tpl');

?>

where the content of index.tpl is:

Hello {$firstname} {$lastname}, glad to see you can make it.
<br />
{* this will not work as $vars are case sensitive *}
This weeks meeting is in {$meetingplace}.
{* this will work *}
This weeks meeting is in {$meetingPlace}.

This will output:

Hello Doug Evans, glad to see you can make it.
<br />
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.

Example 4-2. accessing associative array variables

<?php
$smarty
->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.fax}<br />
{$Contacts.email}<br />
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}<br />
{$Contacts.phone.cell}<br />

this will output:

555-222-9876<br />
zaphod@slartibartfast.example.com<br />
555-444-3333<br />
555-111-1234<br />

Array indexes

You can reference arrays by their index, much like native PHP syntax.

Example 4-3. accessing arrays by index

<?php
$smarty
->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[0]}<br />
{$Contacts[1]}<br />
{* you can print arrays of arrays as well *}
{$Contacts[2][0]}<br />
{$Contacts[2][1]}<br />

This will output:

555-222-9876<br />
zaphod@slartibartfast.example.com<br />
555-444-3333<br />
555-111-1234<br />

Objects

Properties of objects assigned from PHP can be referenced by specifying the property name after the '->' symbol.

Example 4-4. accessing object properties

name: {$person->name}<br />
email: {$person->email}<br />

this will output:

name: Zaphod Beeblebrox<br />
email: zaphod@slartibartfast.example.com<br />
© Copyright 2003-2014 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.