Skip to content

registerDefaultPluginHandler()

注册一个在未定义标签上被调用的函数

描述

void

registerDefaultPluginHandler

mixed

callback

注册一个默认的插件处理器,如果编译器找不到标签的定义,它会被调用。它使用以下参数:

如果在编译过程中 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;
        }
     }

    ?>

注意

返回的回调必须是静态的;一个函数名称或一个类和方法名称的数组。

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