Skip to content

Commit

Permalink
refactor: update the simple template render logic and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 30, 2022
1 parent 9efc93f commit 79e2771
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/SimpleTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +34,7 @@ class SimpleTemplate extends AbstractTemplate
/**
* @var string
*/
private string $formatLeft = '{{';
private string $varLeft = '{{';

/**
* @param string $tplFile
Expand All @@ -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);
}

/**
Expand All @@ -59,22 +57,15 @@ 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;
}

if ($this->globalVars) {
$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);
}

/**
Expand All @@ -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;
}
}
43 changes: 43 additions & 0 deletions test/SimpleTemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace PhpPkg\EasyTplTest;

use PhpPkg\EasyTpl\SimpleTemplate;

/**
* class SimpleTemplateTest
*
* @author inhere
* @date 2022/12/30
*/
class SimpleTemplateTest extends BaseTestCase
{
public function testSimpleTemplate(): void
{
$st = SimpleTemplate::new();
$st->setGlobalVars(['age' => 300]);

$tpl = <<<TPL
Hi, my name is {{ name }}, age is {{ age }}.
first tag: {{ tags.0 }}
user info: {{ user }}
TPL;

$vars = [
'name' => '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);
}
}
3 changes: 3 additions & 0 deletions test/testdata/simple.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hi, my name is {{ name }}, age is {{ age }}.
first tag: {{ tags.0 }}
user sex: {{ user.sex }}

0 comments on commit 79e2771

Please sign in to comment.