Table of Contents
Smarty有多�类型的��。
在Smartyä¸çš„å�˜é‡�å�¯ä»¥ç›´æŽ¥æ˜¾ç¤ºï¼Œæˆ–者作为 函数, 属性 and 修饰器, 内部æ�¡ä»¶è¡¨è¾¾å¼�ç‰çš„å�‚数。 è¦�显示å�˜é‡�,å�¯ä»¥ç®€å�•地用 定界符 把å�˜é‡�括起æ�¥ã€‚
Example 4.1. å�˜é‡�例å�
{$Name}
{$product.part_no} <b>{$product.description}</b>
{$Contacts[row].Phone}
<body bgcolor="{#bgcolor#}">
说明
一个简�的检查Smarty��的方法是打开Smarty的 调试控制�。
赋值的��以美元符� ($) 开头。
Example 4.2. ��赋值
PHP 代ç �
<?php
$smarty = new Smarty();
$smarty->assign('firstname', 'Doug');
$smarty->assign('lastname', 'Evans');
$smarty->assign('meetingPlace', 'New York');
$smarty->display('index.tpl');
?>
index.tpl模æ�¿æº�ç �:
Hello {$firstname} {$lastname}, glad to see you can make it.
<br />
{* this will not work as $variables are case sensitive *}
This weeks meeting is in {$meetingplace}.
{* this will work *}
This weeks meeting is in {$meetingPlace}.
输出:
Hello Doug Evans, glad to see you can make it. <br /> This weeks meeting is in . This weeks meeting is in New York.
�以通过点�“.��使用赋值的数组��。
Example 4.3. 数组��
<?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');
?>
index.tpl模æ�¿ä»£ç �:
{$Contacts.fax}<br />
{$Contacts.email}<br />
{* you can print arrays of arrays as well *}
{$Contacts.phone.home}<br />
{$Contacts.phone.cell}<br />
输出:
555-222-9876<br /> zaphod@slartibartfast.example.com<br /> 555-444-3333<br /> 555-111-1234<br />
ä½ å�¯ä»¥é€šè¿‡ä¸‹æ ‡æ�¥ä½¿ç”¨æ•°ç»„,和PHPè¯æ³•ä¸€æ ·ã€‚
Example 4.4.Â ä½¿ç”¨æ•°ç»„ä¸‹æ ‡
<?php
$smarty->assign('Contacts', array(
'555-222-9876',
'zaphod@slartibartfast.example.com',
array('555-444-3333',
'555-111-1234')
));
$smarty->display('index.tpl');
?>
index.tpl模æ�¿ä»£ç �:
{$Contacts[0]}<br />
{$Contacts[1]}<br />
{* you can print arrays of arrays as well *}
{$Contacts[2][0]}<br />
{$Contacts[2][1]}<br />
输出:
555-222-9876<br /> zaphod@slartibartfast.example.com<br /> 555-444-3333<br /> 555-111-1234<br />
从PHP赋值的对象的属性和方法,�以通过->�使用。
Example 4.5. 使用对象
name: {$person->name}<br />
email: {$person->email}<br />
输出:
name: Zaphod Beeblebrox<br /> email: zaphod@slartibartfast.example.com<br />
