Chapter 4. Variables

Table of Contents
Variables assigned from PHP
Variables loaded from config files
{$smarty} reserved variable

Smarty has several different types of variables. The type of the variable depends on what symbol it is prefixed with (or enclosed within).

Smarty 擁有數種型別的變數,而變數的型別則決定於何種符號先行於變數之前(或包 圍變數)。

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:

Smarty 中的變數可以直接是變數,或是給函式與條件判斷式等等之類使用的參數。如 果想要列印出參數的話則只要用阻斷符號將變數括弧起來即可,例如:
{$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 the template with the assign function are also displayed this way.

PHP 程式碼指定的變數前面都會有一個金錢符號 $,而從其他樣版使用 assign 函式指定的變數其呈現方式如下。

Example 4-1. 指定變數

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

This will output:

將會輸出:

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

你也可以經由指定句號(.)後的關鍵字參照 PHP 程式分配的關聯陣列。

Example 4-2. 取得關聯陣列變數

<?php
$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:

而 index.tpl 的內容如下:

{$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.com<br />
555-444-3333<br />
555-111-1234<br />

Array indexes

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

你可以像 PHP 語法一樣使用索引取得陣列內容。

Example 4-3. 使用索引取得陣列內容

<?php

$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:

而 index.tpl 的內容如:

{$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.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.

PHP 指定的物件屬性可以經由在 '->' 符號後指定屬性名稱的方式參照。

Example 4-4. 取得物件屬性

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

this will output:

輸出內容如:

name: Zaphod Beeblebrox<br />
email: zaphod@slartibartfast.com<br />