<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-&gt;assign('foo','smarty');&#13; &#13; // assign variables to data object scope 为数据对象域分配变量$data = $smarty-&gt;createData();$data-&gt;assign('foo','data');$data-&gt;assign('bar','bar-data');&#13; &#13; // assign variables to other data object scope 为其它数据对象域分配变量$data2 = $smarty-&gt;createData($data);$data2-&gt;assign('bar','bar-data2');&#13; &#13; // assign variable to template object scope 为模版对象域分配变量$tpl = $smarty-&gt;createTemplate('index.tpl');$tpl-&gt;assign('bar','bar-template');&#13; &#13; // assign variable to template object scope with link to Smarty object 用链接至Smarty对象方式指定模版对象的变量范围$tpl2 = $smarty-&gt;createTemplate('index.tpl',$smarty);$tpl2-&gt;assign('bar','bar-template2');&#13; &#13; // This display() does see $foo='smarty' from the $smarty object 在$samrty对象中,执行display(),$foo='smarty'可见$smarty-&gt;display('index.tpl');&#13; &#13; // This display() does see $foo='data' and $bar='bar-data' from the data object $data&#13; //在数据对象$data中,执行display()令$foo='data'和$bar='bar-data'可见$smarty-&gt;display('index.tpl',$data);&#13; &#13; // This display() does see $foo='data' from the data object $data // and $bar='bar-data2' from the data object $data2&#13; //数据对象$data执行display(),$foo='data'可见,$bar='bar-data2'在$data2中可见$smarty-&gt;display('index.tpl',$data2);&#13; &#13; // This display() does see $bar='bar-template' from the template object $tpl //模板对象$tpl执行display(),$bar='bar-template'可见$tpl-&gt;display(); // or $smarty-&gt;display($tpl);&#13; &#13; // This display() does see $bar='bar-template2' from the template object $tpl2// and $foo='smarty' form the Smarty object $foo$tpl2-&gt;display(); // or $smarty-&gt;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>