<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.builtin.functions.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.append.html" accesskey="N">Next</a></td></tr></table> # {$var=...} 变量赋值 This is a short-hand version of the {assign} function. You can assign values directly to the template, or assign values to array elements too. 这是{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> The following attributes can be added to the tag: **属性标签: ** <table width="80%" border="1" class="CALSTABLE"><thead><tr><th width="20%" align="CENTER" valign="TOP">Attribute Name</th> <th width="20%" align="CENTER" valign="TOP">Shorhand</th> <th width="20%" align="CENTER" valign="TOP">Type</th> <th width="20%" align="CENTER" valign="TOP">Required</th> <th width="20%" align="CENTER" valign="TOP">Default</th> <th width="20%" align="LEFT" valign="TOP">Description</th> </tr></thead><tbody><tr><td width="20%" align="CENTER" valign="TOP">scope</td> <td width="20%" align="CENTER" valign="TOP">n/a</td> <td width="20%" align="CENTER" valign="TOP">string</td> <td width="20%" align="CENTER" valign="TOP">no</td> <td width="20%" align="CENTER" valign="TOP"><span class="emphasis"> <i class="EMPHASIS">n/a</i> </span> </td> <td width="20%" align="LEFT" valign="TOP"> The scope of the assigned variable: 'parent','root' or<br/> 'global'</td> </tr></tbody></table> **option flags:** | **Name** | **Description** | |-----|-----| | nocache | Assingns the variable with the 'nocache' attribute | **属性:** | 属性 | 简写 | 类型 | 是否必须 | 缺省值 | 描述 | |-----|-----|-----|-----|-----|-----| | scope | n/a(不可用) | string | no | *n/a* | 指定的变量范围为:parent、root、global | **选项标签:** | **名称** | **描述** | |-----|-----| | nocache | 用'nocache' 属性为变量赋值(意为变量不缓存) | <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN271" id="AEN271"> </a> <b>Example 7.1. Simple assignment<br/> 例 7-1.简单的赋值</b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">{$name='Bob'}The value of $name is {$name}. The above example will output:The value of $name is Bob.</pre> </td> </tr></table></div></td> </tr></table> **Example 7.2. Assignment with math ** **例 7-2.数学计算的赋值** | ~~~ {$running_total=$running_total+$some_array[row].some_value} ~~~ | |-----| **Example 7.3. Assignment of an array element** **例 7-3.数组元素赋值 ** | ~~~ {$user.name="Bob"} ~~~ | |-----| **Example 7.4. Assignment of an multidimensional array element** **例 7-4.多维数组元素的赋值 ** | ~~~ {$user.name.first="Bob"} ~~~ | |-----| **Example 7.5. Appending an array** **例 7-5.为数组添加新元素 ** | ~~~ {$users[]="Bob"} ~~~ | |-----| **Example 7.6. Assigment in the scope of calling template** **例 7-6.为调用的模版指定变量范围** | ~~~ Variables assigned in the included template will be seen in the including template. 被载入的模版变量值只能在该模版里可见(作为调入的模版不可见)。{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(译注:被载入的模版变量)可在载入模版中可见 *}{$foo="something" scope=parent}{* bar is assigned only local in the including template *} {* bar只在载入模版中指定 *}{$bar="value"}... ~~~ | |-----| See also {assign} and {append} 参见[{assign}](#)和[{append}](#) <table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="language.builtin.functions.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.append.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Built-in Functions<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">{append}<br/> 追加</td></tr></table>