<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.syntax.attributes.html" accesskey="P">Prev</a></td> <td width="50%" align="center" valign="bottom">Chapter 3. Basic Syntax[第3章.基本语法]</td> <td width="25%" align="right" valign="bottom"><a href="language.math.html" accesskey="N">Next</a></td></tr></table> # Embedding Vars in Double Quotes[双引号里嵌入变量] Smarty will recognize assigned variables embedded in "double quotes" so long as the variable name contains only numbers, letters and under_scores. See naming [http://php.net/language.variables] for more detail. With any other characters, for example a .period or $object>reference, then the variable must be surrounded by `backticks`. In addition Smarty3 does allow embedded Smarty tags in double quoted strings. This is usefull if you want to include variables with modifers, plugin or PHP function results. Smarty可以识别嵌入在双引号中的变量,只要此变量只包含数字、字母、下划线和中括号[],详细资料参考php[命名](http://php.net/language.variables)。 对于其他的符号(句号、对象引用等等)此变量必须用两个反引号`(此符号和~'在同一个键上,一般在ESC键下面)包住。 Smarty3增加了双引号对Smarty标签的支持。在需要包含调节器变量、插件、php函数返回值的情形中非常实用。 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN194"/> <b><span class="PROGRAMLISTING">Example 3.5. Syntax examples</span><br/></b><strong>例 3-5.语法示例 </strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre>{func var="test $foo test"} // sees $foo{func var="test $foo_bar test"} // sees $foo_bar{func var="test `$foo[0]` test"} // sees $foo[0]{func var="test `$foo[bar]` test"} // sees $foo[bar]{func var="test $foo.bar test"} // sees $foo (not $foo.bar) Smarty只识别变量$foo,其它按原文输出{func var="test `$foo.bar` test"} // sees $foo.bar 加了反引号,Smarty能识别变量$foo.bar{func var="test `$foo.bar` test"|escape} // modifiers outside quotes! 调节器在引号外{func var="test {$foo|escape} test"} // modifiers inside quotes! 调节器在引号内{func var="test {time()} test"} // PHP function result{func var="test {counter} test"} // plugin result{func var="variable foo is {if !$foo}not {/if} defined"} // Smarty block function</pre></td></tr></table><p><b>Example 3.6. Examples<br/></b><strong>例 3-6.示例 </strong></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> {* will replace $tpl_name with value *}<br/> {include file="subdir/$tpl_name.tpl"}<br/> {* does NOT replace $tpl_name *}<br/> {include file='subdir/$tpl_name.tpl'} // vars require double quotes! 需要使用双引号<br/> {* must have backticks as it contains a dot "." *} {* 须用反引号,因为引号里面包含“.” *} <br/> {cycle values="one,two,`$smarty.config.myval`"}<br/> {* must have backticks as it contains a dot "." *}<br/> {include file="`$module.contact`.tpl"}<br/> {* can use variable with dot syntax *}<br/> {include file="`$module.$view`.tpl"}</td> </tr></table><table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td><p>Note<br/> Although Smarty can handle some very complex expressions and syntax, it is a good rule of thumb to keep the template syntax minimal and focused on presentation. If you find your template syntax getting too complex, it may be a good idea to move the bits that do not deal explicitly with presentation to PHP by way of plugins or modifiers.<br/> 虽然Smarty能处理一些复杂的表达式和语法,但从经验上来说的,一个好的做法是最低限度使用模版语法,将其专注于表现外在内容。如果发现你的模版语法太复杂,最好将与外在表现无关的后台处理通过插件或调节器交给php处理。<br/></p> </td> </tr></table><p>See also escape.<br/> 更多相关参考<a href="language.modifier.escape.html">escape</a>。</p></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.syntax.attributes.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.math.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Attributes<br/> 属性 </td> <td width="34%" align="center" valign="top"><a href="language.basic.syntax.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">Math<br/> 数学运算 </td></tr></table>