<table cellspacing="0" cellpadding="0" border="0" width="100%" summary="Header navigation table"><tbody><tr><th align="center" colspan="3">Smarty - the compiling PHPtemplate engine</th> </tr><tr><td valign="bottom" align="left" width="25%"><a accesskey="P" href="api.registerfilter.html">Prev</a></td> <td valign="bottom" align="center" width="50%">Chapter 13. Smarty Class Methods()</td> <td valign="bottom" align="right" width="25%"><a accesskey="N" href="api.register.object.html">Next</a></td> </tr></tbody></table>
# registerPlugin()[注冊插件]
Name
registerPlugin() ― dynamically register plugins 動態注冊插件
Description
void registerPlugin(string type, string name, mixed callback, bool cacheable, mixed cache_attrs);
This method registers functions or methods defined in your script as plugin. It uses the following parameters:
type defines the type of the plugin. Valid values are "function", "block", "compiler" and "modifier".
name defines the name of the plugin.
callback defines the PHP callback. it can be either:
1.A string containing the function name
2.An array of the form array(&$object, $method) with &$object being a reference to an object and $method being a string containing the method-name
3.An array of the form array($class, $method) with $class being the class name and $method being a method of the class.
cacheable and cache_attrs can be omitted in most cases. See controlling cacheability of plugins output on how to use them properly.
本方法在腳本中注冊函數或方法作為插件。其參數如下:
“type”定義插件的類型,其值為下列之一:“function”、“block”、“compiler”和“modifier”;
“name”定義插件的函數名;
“callback”為定義的php回調函數,其類型為下列之一:
1、包含函數名的字符串;
2、格式為(&$object, $method)的數組,其中,&$object為引用對象,$method為包含方法名的字符串;
3、格式為($class, $method)的數組,其中,$class為類名,$method為類中的方法。
“cacheable”和“cache_attrs”參數大多情況下可以省略。參見[如何正確地控制插件緩存輸出](#)。
<table class="EXAMPLE" cellspacing="0" cellpadding="0" border="0" width="100%"><tbody><tr><td> <div class="EXAMPLE"><strong><a name="AEN31327" id="AEN31327"/>Example 13.27. register a function plugin</strong><b><br/>例13-27.注冊一個<b>函數</b>插件<br/></b><table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td> <pre class="PROGRAMLISTING"><?php$smarty->registerPlugin("function","date_now", "print_current_date");function print_current_date($params, $smarty){ if(empty($params["format"])) { $format = "%b %e, %Y"; } else { $format = $params["format"]; } return strftime($format,time());}?> And in the template{date_now}{* or to format differently *} {* 或者運用自己的格式化形式 *}{date_now format="%Y/%m/%d"}</pre></td> </tr></tbody></table><p><span class="PROGRAMLISTING"><strong>Example 13.28. register block function plugin</strong></span><b><br/>例13-28.注冊一個<b>塊函數</b>插件</b></p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><pre class="PROGRAMLISTING"><?php// function declarationfunction do_translation ($params, $content, $smarty, &$repeat, $template){ if (isset($content)) { $lang = $params["lang"]; // do some translation with $content return $translation; }}// register with smarty$smarty->registerPlugin("block","translate", "do_translation");?> Where the template is:{translate lang="br"}Hello, world!{/translate}</pre></td> </tr></tbody></table><p><strong>Example 13.29. register modifier plugin</strong><b><br/>例13-29.注冊一個<b>調節器</b>插件</b></p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td><?php<br/> // let's map PHP's stripslashes function to a Smarty modifier. 讓我們將php的stripslashes()函數映射到Smarty調節器中 <br/> $smarty->registerPlugin("modifier","ss", "stripslashes");<br/> ?><br/> In the template, use ss to strip slashes.<br/> <?php<br/> {$var|ss}<br/> ?></td> </tr></tbody></table><p> 參見<a href="api.unregisterplugin.html">unregisterPlugin()</a>、<a href="plugins.writing.html">插件函數</a>、<a href="plugins.block.functions.html">插件塊函數</a>、<a href="plugins.compiler.functions.html">插件編譯函數</a>和<a href="plugins.modifiers.html">建立調節器插件</a>一節。</p> </div> </td> </tr></tbody></table>
<table cellspacing="0" cellpadding="0" border="0" width="100%" summary="Footer navigation table"><tbody><tr><td valign="top" align="left" width="33%"><a accesskey="P" href="api.registerfilter.html">Prev</a></td> <td valign="top" align="center" width="34%"><a accesskey="H" href="index.html">Home</a></td> <td valign="top" align="right" width="33%"><a accesskey="N" href="api.register.object.html">Next</a></td> </tr><tr><td valign="top" align="left" width="33%">registerFilter()<br/> 注冊過濾器</td> <td valign="top" align="center" width="34%"><a accesskey="U" href="api.functions.html">Up</a></td> <td valign="top" align="right" width="33%">registerObject()<br/> 注冊對象</td> </tr></tbody></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使用指南
- 翻譯人員列表
