registerPlugin()
动态注册插件
说明
void
registerPlugin
string
类型
string
名称
mixed
回调
bool
可缓存的
mixed
cache_attrs
此方法将脚本中定义的函数或方法注册为插件。它使用以下参数
- 在大多数情况下,可以省略
cacheable
。请参阅控制插件输出的可缓存性了解如何正确使用此方法。
<?php
$smarty->registerPlugin("function","date_now", "print_current_date");
function print_current_date($params, $smarty)
{
if(empty($params["format"])) {
$format = "%b %e, %Y";
} else {
$format = $params["format"];
}
return strftime($format,time());
}
?>
在模板中
{date_now}
{* or to format differently *}
{date_now format="%Y/%m/%d"}
<?php
// 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");
?>
模板位于
{translate lang="br"}Hello, world!{/translate}
<?php
// let's map PHP's stripslashes function to a Smarty modifier.
$smarty->registerPlugin("modifier","ss", "stripslashes");
?>
在模板中,使用ss
删除斜杠。
<?php
{$var|ss}
?>
另请参阅unregisterPlugin()
、插件函数、插件块函数、插件编译函数以及创建插件修饰符部分。