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-name

  • An array of the form array($class, $method) with $class being the class name and $method being a method of the class.

当Smarty在编译过程中遇到未定义(没有注册的插件或者不在插件目录下)的标签时,Smarty将试图调用默认的插件处理器来处理。 如果未定义标签是在循环中,则该处理器将有可能被多次调用。


Example 14.38. 默认插件处理器例子

  1. <?php
  2.  
  3. $smarty = new Smarty();
  4. $smarty->registerDefaultPluginHandler('my_plugin_handler');
  5.  
  6. /**
  7. * 默认插件处理器
  8. *
  9. * 当Smarty在编译过程中遇到未定义的标签时调用
  10. *
  11. * @param string $name 未定义标签的名称
  12. * @param string $type 标签类型 (比如: Smarty::PLUGIN_FUNCTION,Smarty::PLUGIN_BLOCK,
  13. Smarty::PLUGIN_COMPILER,Smarty::PLUGIN_MODIFIER,Smarty::PLUGIN_MODIFIERCOMPILER)
  14. * @param Smarty_Internal_Template $template 模板对象
  15. * @param string &$callback 返回 回调函数名
  16. * @param string &$script 当回调函数是外部的,可返回 函数所在脚本的路径。
  17. * @param bool &$cacheable 默认true, 如果插件是不可缓存的设置成false (Smarty >= 3.1.8)
  18. * @return bool 成功返回true
  19. */
  20. function my_plugin_handler ($name, $type, $template, &$callback, &$script, &$cacheable)
  21. {
  22. switch ($type) {
  23. case Smarty::PLUGIN_FUNCTION:
  24. switch ($name) {
  25. case 'scriptfunction':
  26. $script = './scripts/script_function_tag.php';
  27. $callback = 'default_script_function_tag';
  28. return true;
  29. case 'localfunction':
  30. $callback = 'default_local_function_tag';
  31. return true;
  32. default:
  33. return false;
  34. }
  35. case Smarty::PLUGIN_COMPILER:
  36. switch ($name) {
  37. case 'scriptcompilerfunction':
  38. $script = './scripts/script_compiler_function_tag.php';
  39. $callback = 'default_script_compiler_function_tag';
  40. return true;
  41. default:
  42. return false;
  43. }
  44. case Smarty::PLUGIN_BLOCK:
  45. switch ($name) {
  46. case 'scriptblock':
  47. $script = './scripts/script_block_tag.php';
  48. $callback = 'default_script_block_tag';
  49. return true;
  50. default:
  51. return false;
  52. }
  53. default:
  54. return false;
  55. }
  56. }
  57.  
  58. ?>
  59.  

Note

回调方法必须是静态的,如函数名称或者一个包含类和方法名的数组。

不支持如对象方法等动态的回调。

原文: https://www.smarty.net/docs/zh_CN/api.register.default.plugin.handler.tpl