Skip to content

Commit

Permalink
enhance: support find tpl file relative the parent file
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 30, 2022
1 parent 6acef6d commit 8c2beea
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
36 changes: 29 additions & 7 deletions src/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpPkg\EasyTpl\Contract\TemplateInterface;
use Toolkit\FsUtil\File;
use Toolkit\Stdlib\Obj;
use function dirname;
use function is_file;
use function strpos;

Expand Down Expand Up @@ -120,17 +121,38 @@ protected function findTplFile(string $tplName): string
return $this->tplFiles[$tplName];
}

if (!$this->tplDir) {
throw new InvalidArgumentException("no found template file: $tplName");
}

$suffix = '';
$tplFile = $this->resolvePath($this->tplDir) . '/' . $tplName;
if (strpos($tplName, '.') > 0) {
$suffix = File::getExtension($tplName);
}

// is an exists file
// check is an exists file on tplDir
if ($this->tplDir) {
$tplFile = $this->resolvePath($this->tplDir) . '/' . $tplName;
if ($tplFile = $this->tryExistFile($tplFile, $suffix)) {
return $tplFile;
}
}

// try relative the parent tpl file dir.
if ($this->curTplFile) {
$tplFile = dirname($this->curTplFile) . '/' . $tplName;
if ($tplFile = $this->tryExistFile($tplFile, $suffix)) {
return $tplFile;
}
}

throw new InvalidArgumentException("no such template file: $tplName");
}

/**
* @param string $tplFile
* @param string $suffix
*
* @return string
*/
protected function tryExistFile(string $tplFile, string $suffix): string
{
if ($suffix) {
if (is_file($tplFile)) {
return $tplFile;
Expand All @@ -144,7 +166,7 @@ protected function findTplFile(string $tplName): string
}
}

throw new InvalidArgumentException("tplDir: no such template file: $tplName");
return ''; // not exist
}

/**
Expand Down
18 changes: 13 additions & 5 deletions test/EasyTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class EasyTemplateTest extends BaseTestCase
{
private $tplVars = [
private array $tplVars = [
'int' => 23,
'str' => 'a string',
'arr' => [
Expand All @@ -25,9 +25,16 @@ class EasyTemplateTest extends BaseTestCase
],
];

private function newTemplate(): EasyTemplate
{
return new EasyTemplate([
'tmpDir' => $this->getTestdataPath('easy-caches'),
]);
}

public function testCompileCode_check(): void
{
$t2 = new EasyTemplate();
$t2 = $this->newTemplate();

$compiled = $t2->compileCode('');
$this->assertEquals('', $compiled);
Expand All @@ -38,7 +45,7 @@ public function testCompileCode_check(): void

public function testV2RenderFile_use_echo_foreach(): void
{
$t = new EasyTemplate();
$t = $this->newTemplate();

$tplFile = $this->getTestTplFile('testdata/use_echo_foreach.tpl');
$tplVars = ['vars' => ['Info', 'Error', 'Warn']];
Expand Down Expand Up @@ -87,12 +94,13 @@ public function testCompileFile_use_all_token(): void

public function testRenderFile_use_all_token(): void
{
$t = new EasyTemplate();
$t = $this->newTemplate();

$tplFile = $this->getTestTplFile('testdata/use_all_token.tpl');
$result = $t->renderFile($tplFile, $this->tplVars);

$this->assertNotEmpty($result);
$this->assertStringNotContainsString('{{', $result);
vdump($result);
}

Expand All @@ -116,7 +124,7 @@ public function testPhpFuncAsFilter_compile_render(): void

$code = '{{ 34.5 | ceil }}';
$this->assertEquals('<?= htmlspecialchars((string)ceil(34.5)) ?>', $t->compileCode($code));
$this->assertEquals('35', $t->renderString($code, []));
$this->assertEquals('35', $t->renderString($code));
}

public function testAddFilters_compile_render(): void
Expand Down

0 comments on commit 8c2beea

Please sign in to comment.