<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使用指南
- 翻译人员列表