什么是 Smarty
为什么使用它
用例和工作流
语法比较
模板继承
最佳实践
速成教程
registerDefaultPluginHandler() — 注册一个在未定义标签上调用的函数
void registerDefaultPluginHandler(mixed callback);注册一个默认插件处理程序,假如编译器找不到一个定义的话,它会在其他地方被调用。它使用下列参数
callback 定义 PHP 回调。它可以是
包括函数name 的字符串
形式为 array($object, $method) 的数组,其中 $object 引用一个对象,而 $method 包含方法名称的字符串
形式为 array($class, $method) 的数组,其中 $class 是类名,而 $method 是类的方法。
如果 Smarty 编译中遇到内部未定义、已注册或位于插件文件夹中的标签,它会尝试通过调用已注册的默认插件处理程序来解决问题。处理程序可能会被多次调用以针对相同的未定义标签循环遍历有效的插件类型。
示例 14.38 默认插件处理程序示例
<?php
$smarty = new Smarty();
$smarty->registerDefaultPluginHandler('my_plugin_handler');
/**
* Default Plugin Handler
*
* called when Smarty encounters an undefined tag during compilation
*
* @param string $name name of the undefined tag
* @param string $type tag type (e.g. Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK,
Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_MODIFIER, Smarty::PLUGIN_MODIFIERCOMPILER)
* @param Smarty_Internal_Template $template template object
* @param string &$callback returned function name
* @param string &$script optional returned script filepath if function is external
* @param bool &$cacheable true by default, set to false if plugin is not cachable (Smarty >= 3.1.8)
* @return bool true if successfull
*/
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;
}
}
?>
返回的回调必须是静态的:一个函数名称或一个包含类名和方法名的数组。
不支持动态回调,比如对象方法。