smarty3 register_function方法弃用后registerPlugin同时支持块及单函数两用法
register_function 这个函数在 smarty3 中已经被淘汰了,取而代之的是一个名为 registerPlugin 的一个功能更为强大的函数,
该函数的原型为
void registerPlugin(string type,string name,mixed callback,bool cacheable,mixed cache_attrs);
该函数仅仅比register_function多了第一个参数,string type,
文档上是这么定义的:
type defines the type of the plugin. Valid values are “function”, “block”, “compiler” and “modifier”.
翻译过来也就是说该参数定义了插件的类型,有效值为 “function”,”block”,”complier”,”modifier” 中的一个,
在register_function中,只能注册function这一种类型,而该函数可以注册四种类型,其余的用法和register_function基本一致。
举例:
smarty2:
$smarty>register_function(‘format_content’,’htmlcode’);
smarty3:
如果是普通的函数就是
$smarty->registerPlugin(‘function’,’format_content’,’htmlcode’);
如果是块函数就是
$smarty->registerPlugin(‘block’,’format_content’,’htmlcode’);
另外啊这个block我发现如果单纯使用会执行两次 我们需要用if (isset($content)) {加在函数里做判断就只正常执行一次啦
官方手册的写法
// function declaration
function
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"
);
1
|
{translate lang="br"}Hello, world!{/translate} |