模板 -- 变量

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

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

{$Name} 

{$Contacts[row].Phone}

<body bgcolor="{#bgcolor#}">

从PHP分配的变量

Table of Contents[内容列表]
Associative arrays[关联数组]
Array indexes[数组下标]
Objects[对象]

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分配的变量需在前加"$"符号.(译注:同php一样)
调用模板内的assign函数分配的变量也是这样.(译注:也是用$加变量名来调用)

Smarty手册范例 4-1.分配的变量
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}.

输出结果:

Hello Doug, glad to see you could make it.
<p>
Your last login was on January 11th, 2001.

保留变量

The reserved {$smarty} variable can be used to access several special template variables. The full list of them follows.

{$smarty}保留变量可以被用于访问一些特殊的模板变量.
以下是全部列表:

Request variables
The request variables such as get, post, cookies, server, environment, and session variables can be accessed as demonstrated in the examples below:
Example 4-6. displaying request variables
{* 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}

The current timestamp can be accessed with {$smarty.now}. The number reflects the number of seconds passed since the so-called Epoch (January 1, 1970) and can be passed directly to date_format modifier for display purposes.

Example 4-7. using {$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}

You can access PHP constant values directly.

Example 4-8. using {$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.

posted @ 2012-02-14 17:31  haiwei.sun  阅读(227)  评论(0编辑  收藏  举报
返回顶部