Smarty registerPlugin 函数作为参数

Smarty registerPlugin function as argument

在Smarty中,可以通过以下方式注册插件:

$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());
}

参考:https://www.smarty.net/docs/en/api.register.plugin.tpl

但我正在寻找一种可以直接将函数作为参数传递的方法。我怎样才能在 PHP/Smarty 中做到这一点?

例如:

$smarty->registerPlugin("function","date_now", function ($params, $smarty) {
  if(empty($params["format"])) {
    $format = "%b %e, %Y";
  } else {
    $format = $params["format"];
  }
  return strftime($format,time());
});

闭包/lambdas/匿名函数目前不能在 Smarty 模板中使用。

https://www.smarty.net/forums/viewtopic.php?p=73824

dev-master 版本中新增了对闭包的支持。 它将包含在 3.1.28

另读NEW_FEATURES.txt

https://github.com/smarty-php/smarty/issues/59