多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
<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.&#13; 在载入模板中可见被载入模板(译注:即为include进来)的分配变量。{include file="sub_template.tpl"}...{* display variable assigned in sub_template *}{* 在sub_template中可显示赋值的变量 *}{$foo}&lt;br&gt;... The template above includes the example sub_template.tpl below&#13; 上述模版包含下面的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"}...&#13; &#13; 译注:在一些由header.tpl、body.tpl、footer.tpl类型组成的页面中,例如主页是body.tpl,include了&#13; 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.&#13; 通过getTemeplateVars()函数从php脚本中访问{assign}变量。这是个创建$foo变量的模版。{assign var="foo" value="Smarty"}The template variables are only available after/during template execution as in the following script.&#13; 下面的脚本中,模版变量只能在模版执行 后/时 可用。&lt;?php// this will output nothing as the template has not been executedecho $smarty-&gt;getTemplateVars('foo');// fetch the template to a variable 将模版抓取贮存到变量中$whole_page = $smarty-&gt;fetch('index.tpl');// this will output 'smarty' as the template has been executed echo $smarty-&gt;getTemplateVars('foo');$smarty-&gt;assign('foo','Even smarter');// this will output 'Even smarter'echo $smarty-&gt;getTemplateVars('foo');{assign var=foo value="bar" scope="global"}{assign "foo" "bar" scope="global"} {* short-hand *}?&gt;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>