smarty中的变量总共有3种
1,从php分配的变量
2,从配置文件中获取的变量
3,{$smarty}保存的变量
1,从php分配的变量
调用从PHP分配的变量需在前加"$"符号.(译注:同php一样)
调用模板内的assign函数分配的变量也是这样.(译注:也是用$加变量名来调用)例如:
index.php: $smarty = new Smarty; $smarty->assign('firstname', 'Doug'); $smarty->assign('lastLoginDate', 'January 11th, 2001'); $smarty->display('index.tpl'); index.tpl: 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.
关联函数
index.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'); index.tpl: {$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> zaphod@slartibartfast.com<br> 555-444-3333<br> 555-111-1234<br>数组下标
index.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'); index.tpl: {$Contacts[0]}<br> {$Contacts[1]}<br> {* you can print arrays of arrays as well *} {$Contacts[2][0]}<br> {$Contacts[2][1]}<br> OUTPUT: 555-222-9876<br> zaphod@slartibartfast.com<br> 555-444-3333<br> 555-111-1234<br>
2,从配置文件读取的变量
配置文件中的变量需要通过用两个"#"或者是smarty的保留变量 $smarty.config 来调用
第二种语法在变量作为属性值并被引号括住的时候非常有用.
(译注:举个例子 {include file="#includefile#"} 这样#includefile#将被当作字符处理,而不表示配置文件变量,
但可以这样表示 {include file="`$smarty.config.includefile`"}不要忘了加``)配置文件的变量只有在它们被加载以后才能使用.
3,$smarty保留变量
页面请求变量
{* display value of page from URL (GET) http://www.domain.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}$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
{$smarty.const._MY_CONST_VAL}