Skip to content

Commit

Permalink
prof: optimize some template compile logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 10, 2021
1 parent df3752f commit bd2874e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
23 changes: 19 additions & 4 deletions app/Helper/KiteUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
},
]);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Lib/Template/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ abstract class AbstractTemplate implements TemplateInterface
public $pathResolver;

/**
* @param array $config
*
* @return static
*/
public static function new(array $config = []): self
Expand Down
2 changes: 1 addition & 1 deletion app/Lib/Template/Compiler/AbstractCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '(';
Expand Down
4 changes: 2 additions & 2 deletions app/Lib/Template/EasyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit bd2874e

Please sign in to comment.