ECJiaWiki:Smarty:从PHP赋值的变量
跳到导航
跳到搜索
ECJia到家Smarty模版手册导航 |
---|
为什么选择Smarty? |
基本语法-变量 |
基本语法-函数 |
基本语法-属性 |
基本语法-双引号中嵌入变量 |
基本语法-数学计算 |
从PHP赋值的变量 |
Smarty保留变量 |
变量修饰器 |
内置函数 |
自定义函数 |
目录
从PHP赋值的变量
赋值的变量以美元符号 ($) 开头。
变量赋值
- PHP代码:
$this->cs->assign('firstname', 'Doug'); $this->cs->assign('lastname', 'Evans'); $this->cs->assign('meetingPlace', 'New York'); $this->cs->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.
数组赋值
- 可以通过点号“.”来使用赋值的数组变量。
PHP代码:
$this->cs->assign('Contacts', array('fax' => '555-222-9876', 'email' => 'zaphod@slartibartfast.example.com', 'phone' => array('home' => '555-444-3333', 'cell' => '555-111-1234') ) ); $this->cs->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语法一样。
PHP代码:
$this->cs->assign('Contacts', array( '555-222-9876', 'zaphod@slartibartfast.example.com', array('555-444-3333', '555-111-1234') )); $this->cs->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赋值的对象的属性和方法,可以通过->来使用。
PHP代码:
$this->cs->assign('person', $this); $this->cs->display('index.tpl');
index.tpl模板代码:
name: {$person->name}<br /> email: {$person->email}<br />
输出:
name: Zaphod Beeblebrox<br /> email: zaphod@slartibartfast.example.com<br />