在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());
});
答案 0 :(得分:4)
在Smarty模板中,闭包/ lambdas /匿名函数目前无法使用。
https://www.smarty.net/forums/viewtopic.php?p=73824
关闭支持是dev-master版本中的新增功能。 它将包含在3.1.28
中另请参阅NEW_FEATURES.txt