多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
<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.html">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 4. Variables[第四章.变量]</td><td width="25%" align="right" valign="bottom"><a href="language.variables.scopes.html">Next</a></td></tr></table> # [Variables assigned from PHP]()[**从PHP分配的变量**] **Table of Contents[内容列表]**[Associative arrays](#)[关联数组][Array indexes](#)[数组下标][Objects](#)[对象] Variables that are assigned from PHP are referenced by preceding them with a dollar sign $. 调用从PHP分配的变量需在前加"$"符号(译注:同php一样)。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN242" id="AEN242"/> <b>Example 4.2. Assigned variables<br/> 例 4-2.分配的变量</b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p> PHP code:<br/> &lt;?php<br/> $smarty = new Smarty();<br/> $smarty-&gt;assign('firstname', 'Doug');<br/> $smarty-&gt;assign('lastname', 'Evans');<br/> $smarty-&gt;assign('meetingPlace', 'New York');<br/> $smarty-&gt;display('index.tpl');<br/> ?&gt;<br/><br/> index.tpl source:</p> <p>Hello {$firstname} {$lastname}, glad to see you can make it.<br/> &lt;br /&gt;<br/> {* this will not work as $variables are case sensitive *} {* 下行读取不到变量,因为变量区分大小写字台 *} <br/> This weeks meeting is in {$meetingplace}.<br/> {* this will work *}<br/> This weeks meeting is in {$meetingPlace}.<br/><br/> This above would output:<br/> Hello Doug Evans, glad to see you can make it.<br/> &lt;br /&gt;<br/> This weeks meeting is in .<br/> This weeks meeting is in New York.</p></td></tr></table></div></td></tr></table> ### Associative arrays[关联数组] You can also reference associative array variables that are assigned from PHP by specifying the key after the '.' (period) symbol. 你也可以通过句号“.”后接数组键的方式来引用从php分配的关联数组变量。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><b>Example 4-3. accessing associative array variables</b> <br/><strong>例4.3.访问关联数组变量 </strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">index.php:&#13; &#13; $smarty = new Smarty;&#13; $smarty-&gt;assign('Contacts',&#13; array('fax' =&gt; '555-222-9876',&#13; 'email' =&gt; 'zaphod@slartibartfast.com',&#13; 'phone' =&gt; array('home' =&gt; '555-444-3333',&#13; 'cell' =&gt; '555-111-1234')));&#13; $smarty-&gt;display('index.tpl');&#13; &#13; index.tpl:&#13; &#13; {$Contacts.fax}&lt;br&gt;&#13; {$Contacts.email}&lt;br&gt;&#13; {* you can print arrays of arrays as well *} {* 同样可以用于多维数组 *}&#13; {$Contacts.phone.home}&lt;br&gt;&#13; {$Contacts.phone.cell}&lt;br&gt;&#13; &#13; OUTPUT:&#13; &#13; 555-222-9876&lt;br&gt;&#13; zaphod@slartibartfast.com&lt;br&gt;&#13; 555-444-3333&lt;br&gt;&#13; 555-111-1234&lt;br&gt;</pre> </td> </tr></table></div></td> </tr></table> ### Array indexes[数组索引] You can reference arrays by their index, much like native PHP syntax. 你也可以使用php原生语法风格引用数组索引。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <p><b>Example 4-4. accessing arrays by index</b> <br/><strong>例4.4.</strong><strong>通过索引访问数组 </strong> </p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><p>&lt;?php<br/> $smarty-&gt;assign('Contacts', array(<br/> '555-222-9876',<br/> 'zaphod@slartibartfast.example.com',<br/> array('555-444-3333',<br/> '555-111-1234')<br/> ));<br/> $smarty-&gt;display('index.tpl');<br/> ?&gt;<br/><br/> index.tpl source:<br/> {$Contacts[0]}&lt;br /&gt;<br/> {$Contacts[1]}&lt;br /&gt;<br/> {* you can print arrays of arrays as well *}<br/> {$Contacts[2][0]}&lt;br /&gt;<br/> {$Contacts[2][1]}&lt;br /&gt;</p> <p>This will output:<br/> 555-222-9876&lt;br /&gt;<br/> zaphod@slartibartfast.example.com&lt;br /&gt;<br/> 555-444-3333&lt;br /&gt;<br/> 555-111-1234&lt;br /&gt;</p></td> </tr></table></div></td> </tr></table> ### Objects[对象] Properties of objects assigned from PHP can be referenced by specifying the property name after the '->' symbol. 可以通过‘->‘符号后接指定属性名的方式访问php分配的对象属性(这不是php访问对象属性的方法嘛,直说嘛:))。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><b>Example 4-5. accessing object properties</b> <br/><strong>例4.5.访问对象属性</strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">name: {$person-&gt;name}&lt;br&gt;&#13; email: {$person-&gt;email}&lt;br&gt;&#13; &#13; OUTPUT:&#13; &#13; name: Zaphod Beeblebrox&lt;br&gt;&#13; email: zaphod@slartibartfast.com&lt;br&gt;</pre> </td> </tr></table></div></td> </tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="language.variables.html">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html">Home</a></td><td width="33%" align="right" valign="top"><a href="language.variables.scopes.html">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Variables<br/> 变量</td> <td width="34%" align="center" valign="top"><a href="smarty.for.designers.html">Up</a></td> <td width="33%" align="right" valign="top">Variable scopes<br/> 变量范围 </td> </tr></table>