<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="tips.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 18. Tips &amp; Tricks</td><td width="25%" align="right" valign="bottom"><a href="tips.default.var.handling.html" accesskey="N">Next</a></td></tr></table> # Blank Variable Handling[空白变量处理] There may be times when you want to print a default value for an empty variable instead of printing nothing, such as printing &nbsp; so that html table backgrounds work properly. Many would use an {if} statement to handle this, but there is a shorthand way with Smarty, using the default variable modifier. 有时候可能你想打印一个空变量的默认值,而不是空输出,比如输出“&nbsp;”(这是空格符号的实体,属于HTML标记)以便表格背景能够适当地工作。大多数人使用{if}语句来处理这类需要,但这里有一种Smatry的简写方法,那就是使用[default](#)变量调节器。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> “Undefined variable” errors will show if PHP xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx and a variable had not been assigned to Smarty.<br/> 如果php xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(这个是什么跟什么?),同时变量未分配给Smarty,那么会出现“Undefined variable”(变量未定义)错误。</td> </tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN4181" id="AEN4181"/> <b>Example 18-1. Printing &amp;nbsp; when a variable is empty</b><br/><b>例 18-1.输出&amp;nbsp;---如果一个变量是空值</b><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">&#13;{* the long way *}&#13; &#13; {if $title eq ""}&#13; &amp;nbsp;&#13; {else}&#13; {$title}&#13; {/if}&#13; &#13; &#13; {* the short way *}&#13; &#13; {$title|default:"&amp;nbsp;"}</pre></td></tr></table><p>参见<a href="language.modifier.default.html">default</a>调节器和<a href="tips.default.var.handling.html">默认变量处理</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="tips.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="tips.default.var.handling.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top"><p>Tips &amp; Tricks<br/> 使用技巧和经验</p> </td><td width="34%" align="center" valign="top"><a href="appendixes.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top"><p>Default Variable Handling<br/> 默认变量处理</p> </td></tr></table>