<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="plugins.compiler.functions.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 16. Extending Smarty With Plugins<br/></td><td width="25%" align="right" valign="bottom"><a href="plugins.outputfilters.html" accesskey="N">Next</a></td></tr></table>
# Prefilters/Postfilters[預濾器/后濾器]
Prefilter and postfilter plugins are very similar in concept; where they differ is in the execution -- more precisely the time of their execution.
從概念上看,預濾器和后濾器插件很相似;不同之處就在于它們的執行,更確切地說是它們的執行時刻。
> string smarty_prefilter_name($source, &$smarty);
> string $source;
> object &$smarty;
Prefilters are used to process the source of the template immediately before compilation. The first parameter to the prefilter function is the template source, possibly modified by some other prefilters. The plugin is supposed to return the modified source. Note that this source is not saved anywhere, it is only used for compilation.
預濾器用來在編譯之前直接處理模板資源(源文件)。預濾器函數的第一個參數是模板資源,該文件可能被其他預過濾器處理過。此插件將返回修改過的資源。請記住此資源僅用來編譯,它不會在任何地方被保存。
> string smarty_postfilter_name($compiled, &$smarty);
string $compiled;
object &$smarty;
Postfilters are used to process the compiled output of the template (the PHP code) immediately after the compilation is done but before the compiled template is saved to the filesystem. The first parameter to the postfilter function is the compiled template code, possibly modified by other postfilters. The plugin is supposed to return the modified version of this code.
后過濾器用在編譯完成后,編譯模板保存到文件系統前,立即處理模板的編譯輸出(PHP代碼)。預過濾器函數的第一個參數是編譯之后的模板代碼,該代碼可能被其他后過濾器處理過。后濾器插件將返回處理過的php代碼譯本。
<table width="80%" border="0" cellpadding="2" cellspacing="2" class="note"><caption> 譯注 </caption> <tr><td>模板、預過濾器、編譯、后過濾器、保存編譯的關系應該是這樣的:<br/><img src="https://box.kancloud.cn/2015-09-02_55e669a1b872f.gif"/></td> </tr></table>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"><a name="AEN4167" id="AEN4167"/><b>Example 16.7. prefilter plugin<br/>例16-7.預濾器插件</b><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><?php/* * Smarty plugin * ------------------------------------------------------------- * File: prefilter.pre01.php * Type: prefilter * Name: pre01 * Purpose: Convert html tags to be lowercase. * ------------------------------------------------------------- */ function smarty_prefilter_pre01($source, $smarty) { return preg_replace('!<(\w+)[^>]+>!e', 'strtolower("$1")', $source); }?></pre></td></tr></table></div></td></tr></table>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <p><b>Example 16.8. postfilter plugin<br/>例16-8.后濾器插件</b></p> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><?php/* * Smarty plugin * ------------------------------------------------------------- * File: postfilter.post01.php * Type: postfilter * Name: post01 * Purpose: Output code that lists all current template vars. * ------------------------------------------------------------- */ function smarty_postfilter_post01($compiled, $smarty) { $compiled = "<pre>\n<?php print_r(\$this->getTemplateVars()); ?>\n</pre>" . $compiled; return $compiled; }?></pre></td></tr></table><p> 參見<a href="api.registerfilter.html">registerFilter()</a>和<a href="api.unregisterfilter.html">unregisterFilter()</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="plugins.compiler.functions.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="plugins.outputfilters.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Compiler Functions<br/> 編譯函數</td><td width="34%" align="center" valign="top"><a href="plugins.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">Output Filters<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使用指南
- 翻譯人員列表