<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.math.html">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.variables.html" accesskey="N">Next</a></td></tr></table> # Escaping Smarty Parsing[忽略Smarty解析] It is sometimes desirable or even necessary to have Smarty ignore sections it would otherwise parse. A classic example is embedding Javascript or CSS code in a template. The problem arises as those languages use the { and } characters which are also the default delimiters for Smarty. 有时,忽略Smarty对某些语句段的解析很有必要。一种典型的情况是嵌入到模板中的javascript或Css代码。原因在于这些语言使用与Smarty默认定界符‘{’和‘}’一样的符号。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td>Note<br/> A good practice for avoiding escapement altogether is by separating your Javascript/CSS into their own files and use standard HTML methods to access them. This will also take advantage of browser script caching. When you need to embed Smarty variables/functions into your Javascript/<br/> CSS, then the following applies.<br/> 一个避免出现这种情况的好习惯是把你的javascript/css代码分离出来保存成一个独立文件,再用html方法链接到模版中。这样做也有利于浏览器缓存脚本。如果你想把Smarty变量、方法嵌入到javascript/css,请看下面的运用。</td> </tr></table> In Smarty templates, the { and } braces will be ignored so long as they are surrounded by white space. This behavior can be disabled by setting the Smarty class variable $auto_literal to false. 在Smarty模版,如果‘{’和‘}’大括号里包含有空格那么整个{}内容会被忽略,你可以设置Smarty类变量$auto_literal=false来取消这种规则。 **Example 3.8. Using the auto-literal feature** **Example 3.8. 使用auto-literal(自动原义)特性** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <p>&lt;script&gt;<br/> // the following braces are ignored by Smarty 以下大括号的内容会被Smarty忽略<br/> // since they are surrounded by whitespace 因为它们里面有空格<br/> function foobar {<br/> alert('foobar!');<br/> }<br/> // this one will need literal escapement 下面的内容会保持原义输出<br/> {literal}<br/> function bazzy {alert('foobar!');}<br/> {/literal}<br/> &lt;/script&gt;</p> </td></tr></table> {literal}..{/literal} blocks are used for escaping blocks of template logic. You can also escape the braces individually with {ldelim},{rdelim} tags or {$smarty.ldelim},{$smarty.rdelim} variables. Smarty's default delimiters { and } cleanly represent presentational content. However if another set of delimiters suit your needs better, you can change them with Smarty's $left_delimiter and $right_delimiter values. [{literal}](#)...{/literal}块被用来忽略模版语法的解析,你也可以用[{ldelim}](#)、[{rdelim}](#)标签或[{$smarty.ldelim}](#)、[{$smarty.rdelim}](#)变量来忽略个别大括号(译注:后面两种方法主要用来在模版中输出左右大括号)。 Smarty默认定界符‘{’和‘}’简洁地描述具体的内容,然而如果你有更好的定界符设置,也可以用Smarty的[$left_delimiter](#)和[$right_delimiter](#)设置相应的值。 <table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 提示 </caption> <tr><td><p>Note:Changing delimiters affects ALL template syntax and escapement. Be sure to clear out cache and<br/> compiled files if you decide to change them.<br/> 在决定更改影响所有模版语法和忽略解析系统的定界符之前,请确定清除所有缓存和编译文件。</p> </td> </tr></table> **Example 3.9. changing delimiters example** **Example 3.9. 更改定界符例子** <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td> <p>&lt;?php<br/> $smarty-&gt;left_delimiter = '&lt;!--{';<br/> $smarty-&gt;right_delimiter = '}--&gt;';<br/> $smarty-&gt;assign('foo', 'bar');<br/> $smarty-&gt;assign('name', 'Albert');<br/> $smarty-&gt;display('example.tpl');<br/> ?&gt;</p> <p>模版:<br/> Welcome &lt;!--{$name}--&gt; to Smarty<br/> &lt;script language="javascript"&gt;<br/> var foo = &lt;!--{$foo}--&gt;;<br/> function dosomething() {<br/> alert("foo is " + foo);<br/> }<br/> dosomething();<br/> &lt;/script&gt;</p></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.math.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.variables.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Math<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">Variables<br/> 变量 </td></tr></table>