smarty变量--[{$smarty}保留变量]

Request variables[页面请求变量]
{$smarty.now}
{$smarty.const}
{$smarty.capture}
{$smarty.config}
{$smarty.section}
{$smarty.template}
{$smarty.current_dir}
{$smarty.version}
{$smarty.block.child}
{$smarty.block.parent}
{$smarty.ldelim}, {$smarty.rdelim}

可以通过php保留变量{$smarty}访问几个环境和请求变量。列表如下。

Request variables

请求变量诸如$_GET, $_POST,$_COOKIE, $_SERVER, $_ENV and $_SESSION (参考$request_vars_order$request_use_auto_globals) 下面举例说明他们的用法:

Example 4.8. Displaying request variables

{* display value of page from URL ($_GET) http://www.example.com/index.php?page=fo
{$smarty.get.page}
{* display the variable "page" from a form ($_POST['page']) *}
{$smarty.post.page}
{* display the value of the cookie "username" ($_COOKIE['username']) *}
{$smarty.cookies.username}
{* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*}
{$smarty.server.SERVER_NAME}
{* display the system environment variable "PATH" *}
{$smarty.env.PATH}
{* display the php session variable "id" ($_SESSION['id']) *}
{$smarty.session.id}
{* display the variable "username" from merged get/post/cookies/server/env *}
{$smarty.request.username}
提示

Note
For historical reasons {$SCRIPT_NAME} is short-hand for {$smarty.server.SCRIPT_NAME}.
<a href="{$SCRIPT_NAME}?page=smarty">click me</a> <a href="{$smarty.server.SCRIPT_NAME}?page=smarty">click me</a>。
由于(版本的)历史原因,{$SCRIPT_NAME}是{$smarty.server.SCRIPT_NAME}的简写。(参考php手册$_SERVER中的'SCRIPT_NAME'元素)
<a href="{$SCRIPT_NAME}?page=smarty">click me</a>、 <a href="{$smarty.server.SCRIPT_NAME}?page=smarty">click me</a>。

提示
Note
Although Smarty provides direct access to PHP super globals for convenience, it should be used with caution. Specifically, GET and POST and REQUEST are commonly used for presentation purposes, but directly accessing SERVER, ENV, COOKIE and SESSION vars is typically avoided, as this is mixing underlying application code structure into the templates. A good practice is to assign specific needed values to template vars.
尽管Smarty提供了直接访问php超级变量的便利,但仍需谨慎使用。一般来说,GET、POST和REQUEST通常用来直接取值,但更常用的方法是通过访问SERVER、ENV、COOKIE、SESSION变量以防止(不安全值)混进模版底层代码。一个好的习惯是给模板变量赋具体的初始值。

{$smarty.now}

可以通过{$smarty.now}取得当前时间戳。返回自从Unix 纪元(格林威治时间 1970 年1月1日00:00:00)到当前时间的秒数,可以直接通过变量调节器date_format输出显示。应注意的是time()在每次触发时被调用;例如,脚本执行完需要3秒钟,在始末分别调用$smarty.now的话将显示3秒的差异。
{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}

{$smarty.const}

你可以直接访问php常量,参见Smarty常量

<?php
// the constant defined in php
define('MY_CONST_VAL','CHERRIES');
?>
Output the constant in a template with
{$smarty.const.MY_CONST_VAL}

<?php // php定义常量 define('MY_CONST_VAL','CHERRIES');
?>
在模板中用{$smarty.const.MY_CONST_VAL}输出常量 虽然Smarty提供了直接访问php常量的便利,但它(Smarty式的模板常量)通常避免了在模板中混合php的做法,一个好的习惯是给模板变量赋特定的需求值。 {$smarty.capture} 可以通过{$smarty.capture}变量捕获内置的{capture}...{/capture}模版输出。更多介绍参见{capture}一节。 {$smarty.config} {$smarty.config}可以取得配置变量。{$smarty.config.foo}是{#foo#}的同义词。更多介绍参见{config_load}一节。 {$smarty.section} {$smarty.section}用来指向{section}循环的属性,里面包含一些有用的值,比如.first/.index等。 {$smarty.foreach}变量不再是新{foreach}函数的值,但Smarty 2.x版仍然支持foreach风格的语法。 {$smarty.template} 返回经过处理的当前模板名(不包括目录)。 {$smarty.current_dir} 返回经过处理的当前模板目录名。 {$smarty.version} 返回经过编译的Smarty模板版本号。 {$smarty.block.child} 返回子模版文本块。 {$smarty.block.parent} 返回父模版文本块。 {$smarty.ldelim},{$smarty.rdelim} 这两者变量用来打印left-delimiter和right-delimiter的字面值,等同于{ldelim}{rdelim}

 

 



posted @ 2012-03-08 12:31  haiwei.sun  阅读(504)  评论(0编辑  收藏  举报
返回顶部