From 79e277113b6c1095e67289428a5ffbd4eaa0df6c Mon Sep 17 00:00:00 2001 From: Inhere Date: Fri, 30 Dec 2022 21:15:08 +0800 Subject: [PATCH] refactor: update the simple template render logic and add tests --- src/SimpleTemplate.php | 23 ++++++-------------- test/SimpleTemplateTest.php | 43 +++++++++++++++++++++++++++++++++++++ test/testdata/simple.tpl | 3 +++ 3 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 test/SimpleTemplateTest.php create mode 100644 test/testdata/simple.tpl diff --git a/src/SimpleTemplate.php b/src/SimpleTemplate.php index 333d61e..ece4c9b 100644 --- a/src/SimpleTemplate.php +++ b/src/SimpleTemplate.php @@ -10,12 +10,11 @@ namespace PhpPkg\EasyTpl; use InvalidArgumentException; +use Toolkit\Stdlib\Str; use function array_merge; use function explode; use function file_get_contents; -use function sprintf; use function str_contains; -use function strtr; /** * Class SimpleTemplate @@ -35,7 +34,7 @@ class SimpleTemplate extends AbstractTemplate /** * @var string */ - private string $formatLeft = '{{'; + private string $varLeft = '{{'; /** * @param string $tplFile @@ -46,9 +45,8 @@ class SimpleTemplate extends AbstractTemplate public function renderFile(string $tplFile, array $tplVars = []): string { $tplFile = $this->curTplFile = $this->findTplFile($tplFile); - $tplCode = file_get_contents($tplFile); - return $this->renderString($tplCode, $tplVars); + return $this->renderString(file_get_contents($tplFile), $tplVars); } /** @@ -59,7 +57,7 @@ public function renderFile(string $tplFile, array $tplVars = []): string */ public function renderString(string $tplCode, array $tplVars = []): string { - if (!str_contains($tplCode, $this->formatLeft)) { + if (!str_contains($tplCode, $this->varLeft)) { return $tplCode; } @@ -67,14 +65,7 @@ public function renderString(string $tplCode, array $tplVars = []): string $tplVars = array_merge($this->globalVars, $tplVars); } - $fmtVars = []; - foreach ($tplVars as $name => $var) { - $name = sprintf($this->format, (string)$name); - // add - $fmtVars[$name] = $var; - } - - return strtr($tplCode, $fmtVars); + return Str::renderVars($tplCode, $tplVars, $this->format); } /** @@ -91,12 +82,12 @@ public function getFormat(): string public function setFormat(string $format): void { if (!str_contains($format, '%s')) { - throw new InvalidArgumentException('var format must contains %s'); + throw new InvalidArgumentException('template var format must contains %s'); } $this->format = $format; // get left chars [$left, ] = explode('%s', $format); - $this->formatLeft = $left; + $this->varLeft = $left; } } diff --git a/test/SimpleTemplateTest.php b/test/SimpleTemplateTest.php new file mode 100644 index 0000000..637349e --- /dev/null +++ b/test/SimpleTemplateTest.php @@ -0,0 +1,43 @@ +setGlobalVars(['age' => 300]); + + $tpl = << 'inhere', + 'tags' => ['php'], + 'user' => ['sex' => 'man'], + ]; + $str = $st->renderString($tpl, $vars); + $this->assertStringContainsString('name is inhere', $str); + $this->assertStringContainsString('age is 300', $str); + $this->assertStringContainsString('first tag: php', $str); + $this->assertStringContainsString('user info: {sex: man}', $str); + + $str = $st->renderFile($this->getTestdataPath('simple.tpl'), $vars); + $this->assertStringContainsString('name is inhere', $str); + $this->assertStringContainsString('age is 300', $str); + $this->assertStringContainsString('first tag: php', $str); + $this->assertStringContainsString('user sex: man', $str); + } +} diff --git a/test/testdata/simple.tpl b/test/testdata/simple.tpl new file mode 100644 index 0000000..42fcb08 --- /dev/null +++ b/test/testdata/simple.tpl @@ -0,0 +1,3 @@ +Hi, my name is {{ name }}, age is {{ age }}. +first tag: {{ tags.0 }} +user sex: {{ user.sex }}