<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/> <a href="{$SCRIPT_NAME}?page=smarty">click me</a> <a href="{$smarty.server.SCRIPT_NAME}?page=smarty">click me</a>。 <br/> 由于(版本的)歷史原因,{$SCRIPT_NAME}是{$smarty.server.SCRIPT_NAME}的簡寫。(參考php手冊$_SERVER中的'SCRIPT_NAME'元素)<br/> <a href="{$SCRIPT_NAME}?page=smarty">click me</a>、 <a href="{$smarty.server.SCRIPT_NAME}?page=smarty">click me</a>。</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>
- Smarty模板編譯引擎
- 序
- 譯序
- I.開始
- 第一章. 什么是Smarty?
- 第二章. 安裝
- II.模板設計者篇
- 第三章.基本語法
- 注釋
- 變量
- 函數
- 屬性
- 雙引號里嵌入變量
- 數學運算
- 忽略Smarty解析
- 第四章.變量
- 從PHP分配的變量
- 從配置文件讀取的變量
- 變量范圍
- {$smarty}保留變量
- 第五章.變量調節器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.組合修改器
- 第七章.內置函數
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定義函數
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.調試控制臺
- III.模板程序員篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty類變量
- $template_dir
- 第十三章.Smarty類方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.緩存
- 建立緩存
- 多重緩存
- 緩存集合
- 控制插件輸出的可緩存性
- 第十五章.高級特性
- 安全
- 通過模板更改設置
- 模板繼承
- 數據流
- 對象
- 靜態類
- 預過濾器
- 后過濾器
- 輸出過濾器
- 緩存處理函數
- 資源
- 第十六章.以插件擴展Smarty
- 插件如何工作
- 命名約定
- 編寫插件
- 模板函數
- 調節器
- 塊函數
- 編譯函數
- 預濾器/后濾器
- 輸出過濾器
- 資源
- 插入
- Ⅳ.附錄
- 第十七章.疑難解答
- Smarty/PHP 錯誤
- 第十八章.使用技巧和經驗
- 空白變量處理
- 默認變量處理
- 傳遞變量標題給頭模板
- 日期
- WAP/WML
- 組件化模板
- 拒絕電子郵件地址
- 第十九章. 相關資源
- 第二十章. 漏洞
- 3.0安裝包
- 2.x版本升級至3.x版本的提示
- 3.0.x使用指南
- 翻譯人員列表