Tpl::register_modifier(): make name optional and allow to register multiple modifiers in one call

This commit is contained in:
Benjamin Renard 2024-12-16 17:53:11 +01:00
parent d642f3c9dd
commit 46ce4edce0
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -334,11 +334,27 @@ class Tpl {
/** /**
* Register a modifier usable from template files * Register a modifier usable from template files
* @param string $name The modifier name * @param string|callable|array<callable> $name_or_callable The modifier name (or modifier callable)
* @param callable $callable The modifier function * @param callable|null $callable The modifier callable (optional)
* @return void * @return void
*/ */
public static function register_modifier($name, $callable) { public static function register_modifier($name_or_callable, $callable=null) {
if (is_array($name_or_callable) && is_null($callable) && !is_callable($name_or_callable)) {
foreach($name_or_callable as $callable)
self :: register_modifier($callable);
return;
}
$name = (
$callable?
$name_or_callable:
(
is_array($name_or_callable)?
$name_or_callable[1]:
$name_or_callable
)
);
$callable = $callable ?? $name_or_callable;
Log::debug("Register callable %s as modifier %s", format_callable($callable), $name);
self :: $smarty -> registerPlugin("modifier", $name, $callable); self :: $smarty -> registerPlugin("modifier", $name, $callable);
} }