Sunday 24 February 2019

Smarty registerPlugin function as argument

In Smarty, it is possible to register an plugin in this way:

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

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

But I'm looking for a way where I can pass the function direct as argument. How can I do that in 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());
});



from Smarty registerPlugin function as argument

No comments:

Post a Comment