Variables

Smarty has several different types of variables, all of which are explained in more detail below. The type of the variable depends on what symbol it is prefixed with (or enclosed within).

Variable 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 bgcolr="{#bgcolor#}">

Variables assigned from PHP

Variables that are assigned from PHP are referenced by preceding them with a dollar sign $.

Example 3-4. assigned variables

Hello {$firstname}, glad to see you could make it.
<p>
Your last login was on {$lastLoginDate}.

OUTPUT:

Hello Doug, glad to see you could make it.
<p>
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.

Example 3-5. accessing associative array variables

{$Contacts.fax}<br>
{$Contacts.email}<br>
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}<br>
{$Contacts.phone.cell}<br>

OUTPUT:

555-222-9876<br>
[email protected]<br>
555-444-3333<br>
555-111-1234<br>

Array indexes

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

Example 3-6. accessing arrays by index

{$Contacts[0]}<br>
{$Contacts[1]}<br>
{* you can print arrays of arrays as well *}
{$Contacts[0][0]}<br>
{$Contacts[0][1]}<br>

Objects

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

Example 3-7. accessing object properties

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

OUTPUT:

name: Zaphod Beeblebrox<br>
email: [email protected]<br>

Variables loaded from config files

Variables that are loaded from the config files are referenced by enclosing them within hash marks (#).

Example 3-8. config variables

<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
	<td>First</td>
	<td>Last</td>
	<td>Address</td>
</tr>
</table>
</body>
</html>

Config file variables cannot be used until after they are loaded in from a config file. This procedure is explained later in this document under config_load.

{$smarty} reserved variable

The reserved {$smarty} variable can be used to access several special template variables. The full list of them follows.

Request variables

The request variables such as get, post, cookies, server, environment, and session variables can be accessed as demonstrated in the examples below:

Example 3-9. displaying request variables

{* display the variable "page" given in the URL, or from a form using the GET method *}
{$smarty.get.page}

{* display the variable "page" from a form using the POST method *}
{$smarty.post.page}

{* display the value of the cookie "username" *}
{$smarty.cookies.username}

{* display the server variable "SERVER_NAME" *}
{$smarty.server.SERVER_NAME}

{* display the system environment variable "PATH" *}
{$smarty.env.PATH}

{* display the php session variable "id" *}
{$smarty.session.id}

{* display the variable "username" from merged get/post/cookies/server/env *}
{$smarty.request.username}

Current timestamp

The current timestamp can be accessed with {$smarty.now}. The number reflects the number of seconds passed since the so-called Epoch (January 1, 1970) and can be passed directly to date_format modifier for display purposes.

Example 3-10. using {$smarty.now}

{* use the date_format modifier to show current date and time *}
{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}

Output capture buffer

The output captured via {capture}..{/capture} construct can be accessed using {$smarty} variable. See section on capture for an example.

Loop properties

{$smarty} variable can be used to refer to 'section' and 'foreach' loop properties. See docs for section and foreach.