<table summary="Header navigation 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.function.append.html" accesskey="P">Prev</a></td> <td width="50%" align="center" valign="bottom">Chapter 7. Built-in Functions[第七章.內置函數]</td> <td width="25%" align="right" valign="bottom"><a href="language.function.block.html" accesskey="N">Next</a></td></tr></table>
# {assign} 賦值
{assign} is used for assigning template variables during the execution of a template.
{assign}用來在模板運行時為模板變量賦值。
<table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> Assignment of variables in-template is essentially placing application logic into the presentation that may be better handled in PHP. Use at your own discretion.<br/> 在模板里為變量賦值本質上來說是為外觀描述放置應用程序,一般來說變量賦值行為在php代碼中操作比較好。不過,一切由你定奪。</td> </tr></table>
<table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> See also the short-form method of assigning template vars.<br/> 參見模板變量賦值簡寫方法。</td> </tr></table>
**Attribute: **
| Attribute Name | Type | Required | Default | Description |
|-----|-----|-----|-----|-----|
| var | string | yes | *n/a* | The name of the variable being assigned |
| value | string | yes | *n/a* | The value being assigned |
| scope | string | no | *n/a* | The scope of the assigned variable:'parent','root' or 'global' |
**option flags:**
| **Name** | **Description** |
|-----|-----|
| nocache | Assingns the variable with the 'nocache' attribute |
**屬性: **
| 屬性名 | 類型 | 是否必須 | 缺省值 | 描述 |
|-----|-----|-----|-----|-----|
| var | string | yes | *n/a* | 要分配值的變量名 |
| value | string | yes | *n/a* | 分配的值 |
| scope | string | no | *n/a* | 分配的變量范圍:'parent','root' or 'global' |
**選項標簽:**
| **名稱** | **描述** |
|-----|-----|
| nocache | 用'nocache' 屬性為變量賦值 |
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN278" id="AEN278"> </a> <b><span class="PROGRAMLISTING">Example 7.8. {assign}</span><br/> 例 7-8.{assign}</b> 賦值 <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">{assign var="name" value="Bob"}{assign "name" "Bob"} {* short-hand *}{* 簡寫 *}The value of $name is {$name}. The above example will output:上例輸出:The value of $name is Bob.</pre> </td> </tr></table><p><br/><b><span class="PROGRAMLISTING">Example 7.9. {assign} as a nocache variable</span><br/></b> <strong>例 7-9.{assign}作為無緩存變量</strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">{assign var="name" value="Bob" nocache}{assign "name" "Bob" nocache} {* short-hand *}The value of $name is {$name}. The above example will output:The value of $name is Bob.</pre> </td> </tr></table><p><br/><b>Example 7.10. {assign} with some maths<br/>例 7-10.帶方法的{assign}</b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> {assign var=running_total value=$running_total+$some_array[$row].some_value}</td> </tr></table><p><br/><b><span class="PROGRAMLISTING">Example 7.11. {assign} in the scope of calling template</span><br/></b> <strong>例 7-11.{assign}在調用模版的作用域</strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">Variables assigned in the included template will be seen in the including template.
在載入模板中可見被載入模板(譯注:即為include進來)的分配變量。{include file="sub_template.tpl"}...{* display variable assigned in sub_template *}{* 在sub_template中可顯示賦值的變量 *}{$foo}<br>... The template above includes the example sub_template.tpl below
上述模版包含下面的sub_template.tpl例子:...{* foo will be known also in the including template *}{* foo同樣在載入模板中可見 *}{assign var="foo" value="something" scope=parent} //用了scope=parent{* bar is assigned only local in the including template *}{* bar只能在載入模板中可見 *}{assign var="bar" value="value"}...
譯注:在一些由header.tpl、body.tpl、footer.tpl類型組成的頁面中,例如主頁是body.tpl,include了
header.tpl和footer.tpl,則可在header與footer中定義一些變量供body共享使用。</pre> </td> </tr></table><p><br/><b>Example 7.12. {assign} a variable to current scope tree<br/></b> <strong>例 7-12.{assign}一個變量到當前作用域樹</strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>You can assign a variable to root of the current root tree. The variable is seen by all templates using the same root tree.<br/> 你可以在當前樹形結構的‘根’中賦值一個變量。該變量可在所有使用該樹形結構的模版中可見。<br/> {assign var=foo value="bar" scope="root"}</p> </td> </tr></table><p><br/><b>Example 7.13. {assign} a global variable<br/></b> <strong>例 7-13.{assign}作為全局變量</strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <p>A glkobal variable is seen by all templates.<br/> 全局變量在所有模板中可見。<br/> {assign var=foo value="bar" scope="global"}<br/> {assign "foo" "bar" scope="global"} {* short-hand *}</p> </td> </tr></table><p><br/><b><span class="PROGRAMLISTING">Example 7.14. Accessing {assign} variables from a PHP script</span><br/></b> <strong>例 7-14.通過php腳本訪問{assign}變量</strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">To access {assign} variables from a php script use getTemplateVars(). Here's the template that creates the variable $foo.
通過getTemeplateVars()函數從php腳本中訪問{assign}變量。這是個創建$foo變量的模版。{assign var="foo" value="Smarty"}The template variables are only available after/during template execution as in the following script.
下面的腳本中,模版變量只能在模版執行 后/時 可用。<?php// this will output nothing as the template has not been executedecho $smarty->getTemplateVars('foo');// fetch the template to a variable 將模版抓取貯存到變量中$whole_page = $smarty->fetch('index.tpl');// this will output 'smarty' as the template has been executed echo $smarty->getTemplateVars('foo');$smarty->assign('foo','Even smarter');// this will output 'Even smarter'echo $smarty->getTemplateVars('foo');{assign var=foo value="bar" scope="global"}{assign "foo" "bar" scope="global"} {* short-hand *}?>The following functions can also optionally assign template variables.下面的函數也可以選擇性地指定模板變量。{capture}, {include}, {include_php}, {insert}, {counter}, {cycle}, {eval},{fetch}, {math}, {textformat}See also {$var=...}, assign() and getTemplateVars().</pre> </td> </tr></table></div></td> </tr></table>
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="language.function.append.html" accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="language.function.block.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">{append}<br/> 追加</td><td width="34%" align="center" valign="top"><a href="language.builtin.functions.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">{block}<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使用指南
- 翻譯人員列表