Smarty registerPlugin函数作为参数

问题描述 投票:8回答:1

在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中做到这一点?

E.g:

$smarty->registerPlugin("function","date_now", function ($params, $smarty) {
  if(empty($params["format"])) {
    $format = "%b %e, %Y";
  } else {
    $format = $params["format"];
  }
  return strftime($format,time());
});
php smarty smarty3 smarty-plugins
1个回答
4
投票

闭包/ lambda /匿名函数目前在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

© www.soinside.com 2019 - 2024. All rights reserved.