Skip to content

Commit

Permalink
prof: always auto find tpl file on render file
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 11, 2021
1 parent 846abf9 commit 08d2be6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
25 changes: 20 additions & 5 deletions src/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ abstract class AbstractTemplate implements TemplateInterface
protected array $allowExt = ['.php', '.tpl'];

/**
* manual set view files
* manual set template files
*
* @var array
* @var array<string, string>
*/
protected array $tplFiles = [];
public array $tplFiles = [];

/**
* template files dir.
*
* @var string
*/
public string $tplDir = '';
Expand All @@ -48,6 +50,13 @@ abstract class AbstractTemplate implements TemplateInterface
*/
public $pathResolver;

/**
* Current rendering tpl file by {@see findTplFile()}
*
* @var string
*/
protected string $curTplFile = '';

/**
* @param array{tplDir: string, allowExt: array, globalVars: array} $config
*
Expand Down Expand Up @@ -85,8 +94,6 @@ public function configThis(callable $fn): self
*/
public function render(string $tplFile, array $tplVars = []): void
{
$tplFile = $this->findTplFile($tplFile);

echo $this->renderFile($tplFile, $tplVars);
}

Expand Down Expand Up @@ -240,4 +247,12 @@ public function setPathResolver(callable $pathResolver): self
$this->pathResolver = $pathResolver;
return $this;
}

/**
* @return string
*/
public function getCurTplFile(): string
{
return $this->curTplFile;
}
}
18 changes: 4 additions & 14 deletions src/EasyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function __construct(array $config = [])

parent::__construct($config);

// init
$this->init($this->compiler);
}

Expand Down Expand Up @@ -122,6 +123,8 @@ public function renderString(string $tplCode, array $tplVars = []): string
}

/**
* Include render file and output result
*
* @param string $tplFile
* @param array $tplVars
*/
Expand All @@ -132,27 +135,14 @@ protected function include(string $tplFile, array $tplVars = []): void
echo $this->doRenderFile($phpFile, $tplVars);
}

/**
* @param string $tplFile
* @param array $tplVars
*
* @return string
*/
protected function renderInclude(string $tplFile, array $tplVars): string
{
$phpFile = $this->compileFile($tplFile);

return $this->doRenderFile($phpFile, $tplVars);
}

/**
* @param string $tplFile
*
* @return string
*/
public function compileFile(string $tplFile): string
{
$tplFile = $this->findTplFile($tplFile);
$tplFile = $this->curTplFile = $this->findTplFile($tplFile);
$tplCode = file_get_contents($tplFile);
$tmpFile = $this->tmpFilepath(md5($tplCode), File::getName($tplFile, true));

Expand Down
2 changes: 2 additions & 0 deletions src/PhpTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function renderString(string $tplCode, array $tplVars = []): string
*/
public function renderFile(string $tplFile, array $tplVars = []): string
{
$this->curTplFile = $tplFile = $this->findTplFile($tplFile);

return $this->doRenderFile($tplFile, $tplVars);
}

Expand Down
19 changes: 14 additions & 5 deletions src/SimpleTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use InvalidArgumentException;
use function array_merge;
use function file_exists;
use function explode;
use function file_get_contents;
use function sprintf;
use function str_contains;
Expand All @@ -25,6 +25,11 @@ class SimpleTemplate extends AbstractTemplate
*/
protected string $format = '{{%s}}';

/**
* @var string
*/
private string $formatLeft = '{{';

/**
* @param string $tplFile
* @param array $tplVars
Expand All @@ -33,10 +38,7 @@ class SimpleTemplate extends AbstractTemplate
*/
public function renderFile(string $tplFile, array $tplVars = []): string
{
if (!file_exists($tplFile)) {
throw new InvalidArgumentException('No such template file:' . $tplFile);
}

$tplFile = $this->curTplFile = $this->findTplFile($tplFile);
$tplCode = file_get_contents($tplFile);

return $this->renderString($tplCode, $tplVars);
Expand All @@ -50,6 +52,10 @@ public function renderFile(string $tplFile, array $tplVars = []): string
*/
public function renderString(string $tplCode, array $tplVars = []): string
{
if (!str_contains($tplCode, $this->formatLeft)) {
return $tplCode;
}

if ($this->globalVars) {
$tplVars = array_merge($this->globalVars, $tplVars);
}
Expand Down Expand Up @@ -82,5 +88,8 @@ public function setFormat(string $format): void
}

$this->format = $format;
// get left chars
[$left, ] = explode('%s', $format);
$this->formatLeft = $left;
}
}

0 comments on commit 08d2be6

Please sign in to comment.