跳至内容

registerPlugin()

动态注册插件

说明

void

registerPlugin

string

类型

string

名称

mixed

回调

bool

可缓存的

mixed

cache_attrs

此方法将脚本中定义的函数或方法注册为插件。它使用以下参数

<?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()插件函数插件块函数插件编译函数以及创建插件修饰符部分。