Board logo

标题: smarty 变量 [打印本页]

作者: fangzhen    时间: 2011-1-29 22:10     标题: smarty 变量

Smarty有几种不同类型的变量.
变量 的类型取决于它的前缀是什么符号(或者被什么符号包围)

Smarty的变量可以直接被输出或者作为函数属性和修饰符(modifiers)的参数,或者用于内部的条件表达式等等.
如果要输出一个变量,只要用定界符将它括起来就可以.例如:

{$Name} 

{$Contacts[row].Phone}

<body bgcolor="{#bgcolor#}">
1、从配置文件读取的变量
配置文件中的变量需要通过用两个"#"或者是smarty的保留变量 $smarty.config.来调用(下节将讲到)
第二种语法在变量作为属性值并被引号括住的时候非常有用.
(译注:举个例子 {include file="#includefile#"} 这样#includefile#将被当作字符处理,而不表示配置文件变量,
但可以这样表示 {include file="`$smarty.config.includefile`"}不要忘了加``)
例:
foo.conf:

pageTitle = "This is mine"
bodyBgColor = "#eeeeee"
tableBorderSize = "3"
tableBgColor = "#bbbbbb"
rowBgColor = "#cccccc"

index.tpl:

{config_load file="foo.conf"}
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
	<td>First</td>
	<td>Last</td>
	<td>Address</td>
</tr>
</table>
</body>
</html>

index.tpl: (alternate syntax)

{config_load file="foo.conf"}
<html>
<title>{$smarty.config.pageTitle}</title>
<body bgcolor="{$smarty.config.bodyBgColor}">
<table border="{$smarty.config.tableBorderSize}" bgcolor="{$smarty.config.tableBgColor}">
<tr bgcolor="{$smarty.config.rowBgColor}">
	<td>First</td>
	<td>Last</td>
	<td>Address</td>
</tr>
</table>
</body>
</html>


输出结果: (same for both examples)

<html>
<title>This is mine</title>
<body bgcolor="#eeeeee">
<table border="3" bgcolor="#bbbbbb">
<tr bgcolor="#cccccc">
	<td>First</td>
	<td>Last</td>
	<td>Address</td>
</tr>
</table>
</body>
</html>

配置文件的变量只有在它们被加载以后才能使用.
这个过程将在以后 {config_load} . 的章节里说明.

2、{$smarty}保留变量

Request variables[页面请求变量](译注:就是get,post,server,session等变量)
{* 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}
{$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}保留变量可以被用于访问一些特殊的模板变量.

Associative arrays 关联数组

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>

输出结果:

555-222-9876<br>
zaphod@slartibartfast.com<br>
555-444-3333<br>
555-111-1234<br>

Array indexes数据下标

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>

输出结果:

555-222-9876<br>
zaphod@slartibartfast.com<br>
555-444-3333<br>
555-111-1234<br>

Objects对象

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

输出结果:

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

 






欢迎光临 PHP开发笔记 (http://phpvi.com/) Powered by Discuz! 6.1.0