Name
registerDefaultPluginHandler() — 注册默认插件处理器
说明
void registerDefaultPluginHandler(mixed callback);
注册一个默认的插件处理器,当编译程序无法找到模板中标签定义的时候,将调用这个处理器进行回调。 参数:
callback
defines the PHP callback. it can be either:
A string containing the function
name
An array of the form
array(&$object, $method)
with&$object
being a reference to an object and$method
being a string containing the method-nameAn array of the form
array($class, $method)
with$class
being the class name and$method
being a method of the class.
当Smarty在编译过程中遇到未定义(没有注册的插件或者不在插件目录下)的标签时,Smarty将试图调用默认的插件处理器来处理。 如果未定义标签是在循环中,则该处理器将有可能被多次调用。
- <?php
- $smarty = new Smarty();
- $smarty->registerDefaultPluginHandler('my_plugin_handler');
- /**
- * 默认插件处理器
- *
- * 当Smarty在编译过程中遇到未定义的标签时调用
- *
- * @param string $name 未定义标签的名称
- * @param string $type 标签类型 (比如: Smarty::PLUGIN_FUNCTION,Smarty::PLUGIN_BLOCK,
- Smarty::PLUGIN_COMPILER,Smarty::PLUGIN_MODIFIER,Smarty::PLUGIN_MODIFIERCOMPILER)
- * @param Smarty_Internal_Template $template 模板对象
- * @param string &$callback 返回 回调函数名
- * @param string &$script 当回调函数是外部的,可返回 函数所在脚本的路径。
- * @param bool &$cacheable 默认true, 如果插件是不可缓存的设置成false (Smarty >= 3.1.8)
- * @return bool 成功返回true
- */
- function my_plugin_handler ($name, $type, $template, &$callback, &$script, &$cacheable)
- {
- switch ($type) {
- case Smarty::PLUGIN_FUNCTION:
- switch ($name) {
- case 'scriptfunction':
- $script = './scripts/script_function_tag.php';
- $callback = 'default_script_function_tag';
- return true;
- case 'localfunction':
- $callback = 'default_local_function_tag';
- return true;
- default:
- return false;
- }
- case Smarty::PLUGIN_COMPILER:
- switch ($name) {
- case 'scriptcompilerfunction':
- $script = './scripts/script_compiler_function_tag.php';
- $callback = 'default_script_compiler_function_tag';
- return true;
- default:
- return false;
- }
- case Smarty::PLUGIN_BLOCK:
- switch ($name) {
- case 'scriptblock':
- $script = './scripts/script_block_tag.php';
- $callback = 'default_script_block_tag';
- return true;
- default:
- return false;
- }
- default:
- return false;
- }
- }
- ?>
Note
回调方法必须是静态的,如函数名称或者一个包含类和方法名的数组。
不支持如对象方法等动态的回调。
原文: https://www.smarty.net/docs/zh_CN/api.register.default.plugin.handler.tpl