|

Smarty: Variables
Chapter 4. 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:
{$Name}
{$Contacts[row].Phone}
|
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.
Example 4-1. assigned variables
Hello {$firstname}, glad to see you could make it.
Your last login was on {$lastLoginDate}.
|
This will output:
Hello Doug, glad to see you could make it.
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 4-2. accessing associative array variables
$smarty
= new
Smarty;
$smarty->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.fax}
{$Contacts.email}
{* you can print arrays of arrays as well *} {$Contacts.phone.home}
{$Contacts.phone.cell}
|
this will output:
555-222-9876
zaphod@slartibartfast.com
555-444-3333
555-111-1234
|
|
Array indexes
You can reference arrays by their index, much like native PHP syntax.
Example 4-3. accessing arrays by index
$smarty
= new
Smarty;
$smarty->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[0]}
{$Contacts[1]}
{* you can print arrays of arrays as well *} {$Contacts[2][0]}
{$Contacts[2][1]}
|
This will output:
555-222-9876
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.
Example 4-4. accessing object properties
name: {$person->name}
email: {$person->email}
|
this will output:
name: Zaphod Beeblebrox
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.
Example 4-5. config variablesfoo.conf:
pageTitle = "This is mine" bodyBgColor = "#eeeeee" tableBorderSize = "3" tableBgColor = "#bbbbbb" rowBgColor = "#cccccc"
|
index.tpl:
{config_load file="foo.conf"}
{#pageTitle#}
|
index.tpl: (alternate syntax)
{config_load file="foo.conf"}
{$smarty.config.pageTitle}
|
this will output for both examples:
|
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 4-6. displaying request variables
{* display value of page from URL (GET) http://www.example.com/index.php?page=foo *} {$smarty.get.page}
{* display the variable "page" from a form (POST) *} {$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}
|
|
Note: For historical reasons {$SCRIPT_NAME} can be
accessed directly though {$smarty.server.SCRIPT_NAME} is the
proposed way to access this value.
{$smarty.now}
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 4-7. using {$smarty.now}
{* use the date_format modifier to show current date and time *} {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}
|
|
{$smarty.const}
You can access PHP constant values directly.
Example 4-8. using {$smarty.const}
{$smarty.const._MY_CONST_VAL}
|
|
{$smarty.capture}
The output captured via {capture}..{/capture} construct can be accessed
using {$smarty} variable. See section on capture for an example.
{$smarty.config}
{$smarty} variable can be used to refer to loaded config variables. {$smarty.config.foo}
is a synonym for {#foo#}. See the section on config_load for an example.
{$smarty.section}, {$smarty.foreach}
{$smarty} variable can be used to refer to \\'section\\' and \\'foreach\\' loop
properties. See docs for section and foreach.
{$smarty.template}
This variable contains the name of the current template being processed.
{$smarty.version}
This variable contains the version of Smarty the template was compiled
with.
{$smarty.ldelim}
This variable is used for printing the left-delimiter value literally.
See also {ldelim},{rdelim}.
{$smarty.rdelim}
This variable is used for printing the right-delimiter value literally.
See also {ldelim},{rdelim}.
|