<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.variables.assigned.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 4. Variables[第四章.變量]</td><td width="25%" align="right" valign="bottom"><a href="language.config.variables.html" accesskey="N">Next</a></td></tr></table>
### Variable scopes[變量范圍]
You have the choice to assign variables to the scope of the main Smarty object, data objects created with createData(), and template objects created with createTemplate(). These objects can be chained. A template sees all the variables of its own object and all variables assigned to the objects in its chain of parent objects.
By default templates which are rendered by $smarty->display(...) or $smarty->fetch(...) calls are automatically linked to the Smarty object variable scope.
By assigning variables to individual data or template objects you have full control which variables can be seen by a template.
你可以選擇為主要的Smarty對象作用域分配變量,createData()用來建立數據對象,createTemplate()用來建立模板對象。這些對象支持鏈式,在模板中可以查看所有模板本身的對象變量和所有分配給父對象鏈的變量。
默認情況下,模板在執行$smarty->displaty(...)、$smarty->fetch(...)方法時已自動鏈接至Smarty對象變量范圍。
對于分配到單個數據或模板對象的變量,您可以完全控制哪些變量在模板中可見。
譯注
| 這些所謂的主要Smarty對象,共有三個,分別是Smarty、模板、數據;在php程序中,為這些作用域變量賦值時,首先須將它們實例化,如$smarty = New Smarty()、$tpl = createTemplate()、$data = createData()。 |
|-----|
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN2194" id="AEN2194"/><b>Example 4.6. Variable scope examples</b><br/><strong>例4.6. 變量范圍例子</strong><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">// assign variable to Smarty object scope 為Smarty對象域分配變量$smarty->assign('foo','smarty');
// assign variables to data object scope 為數據對象域分配變量$data = $smarty->createData();$data->assign('foo','data');$data->assign('bar','bar-data');
// assign variables to other data object scope 為其它數據對象域分配變量$data2 = $smarty->createData($data);$data2->assign('bar','bar-data2');
// assign variable to template object scope 為模版對象域分配變量$tpl = $smarty->createTemplate('index.tpl');$tpl->assign('bar','bar-template');
// assign variable to template object scope with link to Smarty object 用鏈接至Smarty對象方式指定模版對象的變量范圍$tpl2 = $smarty->createTemplate('index.tpl',$smarty);$tpl2->assign('bar','bar-template2');
// This display() does see $foo='smarty' from the $smarty object 在$samrty對象中,執行display(),$foo='smarty'可見$smarty->display('index.tpl');
// This display() does see $foo='data' and $bar='bar-data' from the data object $data
//在數據對象$data中,執行display()令$foo='data'和$bar='bar-data'可見$smarty->display('index.tpl',$data);
// This display() does see $foo='data' from the data object $data // and $bar='bar-data2' from the data object $data2
//數據對象$data執行display(),$foo='data'可見,$bar='bar-data2'在$data2中可見$smarty->display('index.tpl',$data2);
// This display() does see $bar='bar-template' from the template object $tpl //模板對象$tpl執行display(),$bar='bar-template'可見$tpl->display(); // or $smarty->display($tpl);
// This display() does see $bar='bar-template2' from the template object $tpl2// and $foo='smarty' form the Smarty object $foo$tpl2->display(); // or $smarty->display($tpl2);</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.variables.assigned.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.config.variables.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Variables<br/> 變量</td><td width="34%" align="center" valign="top"><a href="language.variables.html#LANGUAGE.ASSIGNED.VARIABLES" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">Variables loaded from config files<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使用指南
- 翻譯人員列表