<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><th colspan="3" align="center">Smarty - the compiling PHP template engine</th></tr><tr><td width="25%" align="left" valign="bottom"><a href="language.config.variables.html">Prev</a></td> <td width="50%" align="center" valign="bottom">Chapter 4. Variables[第四章.变量]</td> <td width="25%" align="right" valign="bottom"><a href="language.modifiers.html">Next</a></td></tr></table> [{$smarty} reserved variable]()[{$smarty}保留变量] **Table of Contents 内容列表**[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}](#) The PHP reserved {$smarty} variable can be used to access several environment and request variables.The full list of them follows. 可以通过php保留变量{$smarty}访问几个环境和请求变量。列表如下。 Request variables The request variables [http://php.net/reserved.variables] such as $_GET, $_POST,$_COOKIE, $_SERVER, $_ENV and $_SESSION (see $request_vars_order and $request_use_auto_globals) can be accessed as demonstrated in the examples below: [请求变量](http://php.net/reserved.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} ~~~ | |-----| <table width="80%" border="0" cellpadding="2" cellspacing="2"><span class="note"> <caption> 提示 </caption> <tr><td><p>Note<br/> For historical reasons {$SCRIPT_NAME} is short-hand for {$smarty.server.SCRIPT_NAME}.<br/> &lt;a href="{$SCRIPT_NAME}?page=smarty"&gt;click me&lt;/a&gt; &lt;a href="{$smarty.server.SCRIPT_NAME}?page=smarty"&gt;click me&lt;/a&gt;。 <br/> 由于(版本的)历史原因,{$SCRIPT_NAME}是{$smarty.server.SCRIPT_NAME}的简写。(参考php手册$_SERVER中的'SCRIPT_NAME'元素)<br/> &lt;a href="{$SCRIPT_NAME}?page=smarty"&gt;click me&lt;/a&gt;、 &lt;a href="{$smarty.server.SCRIPT_NAME}?page=smarty"&gt;click me&lt;/a&gt;。</p> </td> </tr></span></table> <table width="80%" border="0" cellpadding="2" cellspacing="2"><span class="note"> <caption> 提示 </caption> <tr><td>Note<br/> 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.<br/> 尽管Smarty提供了直接访问php超级变量的便利,但仍需谨慎使用。一般来说,GET、POST和REQUEST通常用来直接取值,但更常用的方法是通过访问SERVER、ENV、COOKIE、SESSION变量以防止(不安全值)混进模版底层代码。一个好的习惯是给模板变量赋具体的初始值。</td> </tr></span></table>   {$smarty.now} The current timestamp [http://php.net/function.time] can be accessed with {$smarty.now}. The value reflects the number of seconds passed since the so-called Epoch on January 1, 1970, and can be passed directly to the date_format modifier for display. Note that time() [http://php.net/function.time] is called on each invocation; eg a script that takes three seconds to execute with a call to $smarty.now at start and end will show the three second difference. {* use the date_format modifier to show current date and time *} 可以通过{$smarty.now}取得当前[时间戳](http://php.net/function.time)。返回自从Unix 纪元(格林威治时间 1970 年1月1日00:00:00)到当前时间的秒数,可以直接通过变量调节器[date_format](#)输出显示。应注意的是[time()](http://php.net/function.time)在每次触发时被调用;例如,脚本执行完需要3秒钟,在始末分别调用$smarty.now的话将显示3秒的差异。 {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}   {$smarty.const} You can access PHP constant values directly. See also smarty constants. 你可以直接访问php常量,参见[Smarty常量](#)。 | ~~~ <?php// the constant defined in phpdefine('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}输出常量 ~~~ | |-----| <table width="80%" border="0" cellpadding="2" cellspacing="2"><span class="note"> <caption> 提示 </caption> <tr><td>Note<br/> Although Smarty provides direct access to PHP constants for convenience, it 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. <br/> 虽然Smarty提供了直接访问php常量的便利,但它(Smarty式的模板常量)通常避免了在模板中混合php的做法,一个好的习惯是给模板变量赋特定的需求值。</td> </tr></span></table>   {$smarty.capture} Template output captured via the built-in {capture}..{/capture} function can be accessed usingthe {$smarty.capture} variable. See the {capture} page for more information. 可以通过{$smarty.capture}变量捕获内置的[{capture}](#)...{/capture}模版输出。更多介绍参见[{capture}](#)一节。   {$smarty.config} {$smarty.config} variable can be used to refer to loaded config variables. {$smarty.config.foo} is a synonym for {#foo#}. See the {config_load} page for more info. {$smarty.config}可以取得配置变量。{$smarty.config.foo}是{#foo#}的同义词。更多介绍参见[{config_load}](#)一节。   {$smarty.section} The {$smarty.section} variables can be used to refer to {section} loop properties. These have some very useful values such as .first, .index, etc. {$smarty.section}用来指向[{section}](#)循环的属性,里面包含一些有用的值,比如.first/.index等。 <table width="80%" border="0" cellpadding="2" cellspacing="2"><span class="note"> <caption> 提示 </caption> <tr><td>Note<br/> The {$smarty.foreach} variable is no longer used with the new {foreach} syntax, but is still supported with Smarty 2.x style foreach syntax.<br/> {$smarty.foreach}变量不再是新{foreach}函数的值,但Smarty 2.x版仍然支持foreach风格的语法。</td> </tr></span></table>   {$smarty.template} Returns the name of the current template being processed (without the directory.) 返回经过处理的当前模板名(不包括目录)。   {$smarty.current_dir} Returns the name of the directory for the current template being processed. 返回经过处理的当前模板目录名。   **{$smarty.version}** Returns the version of Smarty the template was compiled with. 返回经过编译的Smarty模板版本号。 | <div id="footer">Powered by Smarty {$smarty.version}</div> | |-----|   {$smarty.block.child} Returns block text from child template. See Template interitance. 返回子模版文本块。参见[模板继承](#)。   {$smarty.block.parent} Returns block text from parent template. See Template interitance 返回父模版文本块。参见[模板继承](#)。   {$smarty.ldelim}, {$smarty.rdelim} These variables are used for printing the left-delimiter and right-delimiter value literally, the same as {ldelim},{rdelim}. See also assigned variables and config variables 这两者变量用来打印left-delimiter和right-delimiter的字面值,等同于[{ldelim}](#)、[{rdelim}](#)。 另参阅[assigned](#)变量和[config](#)变量。 <table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="language.config.variables.html">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html">Home</a></td><td width="33%" align="right" valign="top"><a href="language.modifiers.html">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Variables loaded from config files<br/> 从配置文件读取的变量</td> <td width="34%" align="center" valign="top"><a href="language.variables.html">Up</a></td> <td width="33%" align="right" valign="top">Variable Modifiers<br/> 变量调节器</td> </tr></table>