diff --git a/app/Helper/KiteUtil.php b/app/Helper/KiteUtil.php index a5cd389..f359216 100644 --- a/app/Helper/KiteUtil.php +++ b/app/Helper/KiteUtil.php @@ -4,7 +4,6 @@ use Inhere\Kite\Kite; use Inhere\Kite\Lib\Template\EasyTemplate; -use Leuffen\TextTemplate\TextTemplate; use Toolkit\FsUtil\FS; use Toolkit\Stdlib\OS; use Toolkit\Stdlib\Str; @@ -93,15 +92,31 @@ public static function userConfigDir(string $path = ''): string } /** + * @param array $config + * * @return EasyTemplate */ - public static function newTplEngine(): EasyTemplate + public static function newTplEngine(array $config = []): EasyTemplate { - return EasyTemplate::new() + return EasyTemplate::new($config) + ->disableEchoFilter() ->setPathResolver([Kite::class, 'resolve']) ->configThis(function (EasyTemplate $tpl) { $tpl->tmpDir = Kite::getTmpPath('tplCache'); - }); + }) + ->addFilters([ + 'upFirst' => 'ucfirst', + 'upper' => 'strtoupper', + 'nl' => function (string $str): string { + return $str . "\n"; + }, + 'camel' => function (string $str, bool $upFirst): string { + return Str::toCamel($str, $upFirst); + }, + 'snake' => function (string $str): string { + return Str::toSnake($str); + }, + ]); } /** diff --git a/app/Lib/Template/AbstractTemplate.php b/app/Lib/Template/AbstractTemplate.php index a6e3c52..a93571d 100644 --- a/app/Lib/Template/AbstractTemplate.php +++ b/app/Lib/Template/AbstractTemplate.php @@ -49,6 +49,8 @@ abstract class AbstractTemplate implements TemplateInterface public $pathResolver; /** + * @param array $config + * * @return static */ public static function new(array $config = []): self diff --git a/app/Lib/Template/Compiler/AbstractCompiler.php b/app/Lib/Template/Compiler/AbstractCompiler.php index c8255c8..d35dc04 100644 --- a/app/Lib/Template/Compiler/AbstractCompiler.php +++ b/app/Lib/Template/Compiler/AbstractCompiler.php @@ -111,7 +111,7 @@ protected function parseInlineFilters(string $echoBody): string if (strlen($argStr) > 1 && str_contains($argStr, ',')) { $args = Str::toTypedList($argStr); } else { - $args = [Str::toTyped($argStr)]; + $args = [Str::toTyped($argStr, true)]; } $filter = $this->filterMapping[$filter] ?? $filter . '('; diff --git a/app/Lib/Template/EasyTemplate.php b/app/Lib/Template/EasyTemplate.php index 075bdcf..dfc986b 100644 --- a/app/Lib/Template/EasyTemplate.php +++ b/app/Lib/Template/EasyTemplate.php @@ -144,8 +144,8 @@ public function compileCode(string $code): string */ public function applyFilter(string $filter, string $result, ...$args): string { - if (isset($this->customFilters[$filter])) { - throw new InvalidArgumentException("apply unregistered filter: $filter"); + if (!isset($this->customFilters[$filter])) { + throw new InvalidArgumentException("Template - apply unregistered filter: $filter"); } $filterFn = $this->customFilters[$filter];