<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><script><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/> </script></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><?php<br/> $smarty->left_delimiter = '<!--{';<br/> $smarty->right_delimiter = '}-->';<br/> $smarty->assign('foo', 'bar');<br/> $smarty->assign('name', 'Albert');<br/> $smarty->display('example.tpl');<br/> ?></p> <p>模版:<br/> Welcome <!--{$name}--> to Smarty<br/> <script language="javascript"><br/> var foo = <!--{$foo}-->;<br/> function dosomething() {<br/> alert("foo is " + foo);<br/> }<br/> dosomething();<br/> </script></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>
- Smarty模板編譯引擎
- 序
- 譯序
- I.開始
- 第一章. 什么是Smarty?
- 第二章. 安裝
- II.模板設計者篇
- 第三章.基本語法
- 注釋
- 變量
- 函數
- 屬性
- 雙引號里嵌入變量
- 數學運算
- 忽略Smarty解析
- 第四章.變量
- 從PHP分配的變量
- 從配置文件讀取的變量
- 變量范圍
- {$smarty}保留變量
- 第五章.變量調節器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.組合修改器
- 第七章.內置函數
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定義函數
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.調試控制臺
- III.模板程序員篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty類變量
- $template_dir
- 第十三章.Smarty類方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.緩存
- 建立緩存
- 多重緩存
- 緩存集合
- 控制插件輸出的可緩存性
- 第十五章.高級特性
- 安全
- 通過模板更改設置
- 模板繼承
- 數據流
- 對象
- 靜態類
- 預過濾器
- 后過濾器
- 輸出過濾器
- 緩存處理函數
- 資源
- 第十六章.以插件擴展Smarty
- 插件如何工作
- 命名約定
- 編寫插件
- 模板函數
- 調節器
- 塊函數
- 編譯函數
- 預濾器/后濾器
- 輸出過濾器
- 資源
- 插入
- Ⅳ.附錄
- 第十七章.疑難解答
- Smarty/PHP 錯誤
- 第十八章.使用技巧和經驗
- 空白變量處理
- 默認變量處理
- 傳遞變量標題給頭模板
- 日期
- WAP/WML
- 組件化模板
- 拒絕電子郵件地址
- 第十九章. 相關資源
- 第二十章. 漏洞
- 3.0安裝包
- 2.x版本升級至3.x版本的提示
- 3.0.x使用指南
- 翻譯人員列表