8

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

1 回答 1

4

闭包 / 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

于 2019-02-23T19:47:33.043 回答