You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been catching up with Smarty 5 changes, and how PHP functions won't be available any more. This decision makes sense in some ways, but for other developers it's just a bit of an annoyance - especially if the security issues are irrelevant in your environment.
So here's a tiny bit of code that will essentially register all internal PHP functions as Smarty functions:
$smarty = new Smarty\Smarty();
foreach (get_defined_functions()["internal"] as $php_function) {
$smarty->registerPlugin("function", $php_function, function ($params) use ($php_function) {
return $php_function(...array_values($params));
});
}
... in case you're feeling lazy, and just want the functionality back :)
(Note: you can't use named arguments with this method, so you'd have to supply all the ones you want in the correct order.)
It's probably more efficient from a code point of view to replace get_defined_functions()["internal"] with an array of just the function names you actually want, however!
Maybe this wouldn't be desired/don't want to encourage it with Smarty, but I did think given how simple an addition it would be, there could be an option in Smarty to simply do this, for people who want it. Otherwise, maybe this code will be useful to anyone searching for it!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I've been catching up with Smarty 5 changes, and how PHP functions won't be available any more. This decision makes sense in some ways, but for other developers it's just a bit of an annoyance - especially if the security issues are irrelevant in your environment.
So here's a tiny bit of code that will essentially register all internal PHP functions as Smarty functions:
... in case you're feeling lazy, and just want the functionality back :)
(Note: you can't use named arguments with this method, so you'd have to supply all the ones you want in the correct order.)
It's probably more efficient from a code point of view to replace
get_defined_functions()["internal"]
with an array of just the function names you actually want, however!Maybe this wouldn't be desired/don't want to encourage it with Smarty, but I did think given how simple an addition it would be, there could be an option in Smarty to simply do this, for people who want it. Otherwise, maybe this code will be useful to anyone searching for it!
Beta Was this translation helpful? Give feedback.
All reactions