Skip to content

Commit

Permalink
refactor: update the template interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 6, 2023
1 parent a3adea0 commit 3a85651
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 10 deletions.
22 changes: 21 additions & 1 deletion src/Concern/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,31 @@ public function configThis(callable $fn): static
* @param string $tplFile
* @param array $tplVars
*/
public function render(string $tplFile, array $tplVars = []): void
public function display(string $tplFile, array $tplVars = []): void
{
echo $this->render($tplFile, $tplVars);
}

/**
* @param string $tplFile
* @param array $tplVars
*/
public function displayFile(string $tplFile, array $tplVars = []): void
{
echo $this->renderFile($tplFile, $tplVars);
}

/**
* @param string $tplFile
* @param array $tplVars
*
* @return string
*/
public function render(string $tplFile, array $tplVars = []): string
{
return $this->renderFile($tplFile, $tplVars);
}

/**
* @param string $tplName
*
Expand Down
28 changes: 24 additions & 4 deletions src/Contract/TemplateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,38 @@
*/
interface TemplateInterface
{
/**
* Render template file and output
/*
* Render template file and output result.
*
* @param string $tplFile
* @param array $tplVars
*
* @return void
*/
public function display(string $tplFile, array $tplVars): void;

/*
* Render template file and output result.
*
* @param string $tplFile
* @param array $tplVars
*
* @return void
*/
public function render(string $tplFile, array $tplVars): void;
public function displayFile(string $tplFile, array $tplVars): void;

/**
* Render template file to string.
*
* @param string $tplFile
* @param array $tplVars
*
* @return string
*/
public function render(string $tplFile, array $tplVars): string;

/**
* Render template file to string
* Render template file to string.
*
* @param string $tplFile
* @param array $tplVars
Expand Down
69 changes: 64 additions & 5 deletions src/EasyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpPkg\EasyTpl\Concern\CompiledTemplateTrait;
use PhpPkg\EasyTpl\Contract\CompilerInterface;
use PhpPkg\EasyTpl\Contract\EasyTemplateInterface;
use function str_replace;

/**
* Class EasyTemplate
Expand Down Expand Up @@ -69,6 +70,8 @@ class EasyTemplate extends PhpTemplate implements EasyTemplateInterface
{
use CompiledTemplateTrait;

public const MAIN_CONTENT_MARK = '{_MAIN_CONTENT_}';

/**
* @var string[]
*/
Expand All @@ -87,6 +90,7 @@ class EasyTemplate extends PhpTemplate implements EasyTemplateInterface
* @var string
*/
private string $currentLayout = '';
private string $layoutContent = '';

/**
* Create a text template engine instance.
Expand Down Expand Up @@ -142,18 +146,50 @@ protected function initDirectives(CompilerInterface $compiler): void
/** will call {@see include()} */
return '$this->include' . $body;
})
// use layout file. syntax: {{ layout('layouts/main.tpl') }}
// use layout file. syntax: {{ layout('layouts/main.tpl', ['name' => 'inhere']) }}
->addDirective('layout', function (string $body) {
/** will call {@see useLayout()} */
return '$this->useLayout' . $body;
})
// use on layout file. syntax: {{ contents }}
->addDirective('contents', function () {
return '{_MAIN_CONTENT_}';
});
->addDirective('contents', fn() => self::MAIN_CONTENT_MARK);
}

/**
* Render template file, support `layout()` and $this->defaultLayout.
*
* @param string $tplFile
* @param array $tplVars
*
* @return string
*/
public function render(string $tplFile, array $tplVars = []): string
{
$phpFile = $this->compileFile($tplFile);
$contents = $this->doRenderFile($phpFile, $tplVars);

$useLayout = false;
if ($this->currentLayout) {
$useLayout = true;
} elseif ($this->defaultLayout) {
$useLayout = true;
$this->useLayout($this->defaultLayout);
}

// use layout
if ($useLayout) {
$contents = str_replace(self::MAIN_CONTENT_MARK, $contents, $this->layoutContent);

// reset context
$this->currentLayout = $this->layoutContent = '';
}

return $contents;
}

/**
* Render view file, support layout(), but not apply $this->defaultLayout.
*
* @param string $tplFile
* @param array $tplVars
*
Expand All @@ -162,8 +198,17 @@ protected function initDirectives(CompilerInterface $compiler): void
public function renderFile(string $tplFile, array $tplVars = []): string
{
$phpFile = $this->compileFile($tplFile);
$contents = $this->doRenderFile($phpFile, $tplVars);

// use layout
if ($this->currentLayout) {
$contents = str_replace(self::MAIN_CONTENT_MARK, $contents, $this->layoutContent);

return $this->doRenderFile($phpFile, $tplVars);
// reset context
$this->currentLayout = $this->layoutContent = '';
}

return $contents;
}

/**
Expand Down Expand Up @@ -193,6 +238,20 @@ protected function include(string $tplFile, array $tplVars = []): void
echo $this->doRenderFile($phpFile, $tplVars);
}

/**
* use and render layout file
*
* @param string $layoutFile
* @param array $tplVars
*
* @return void
*/
protected function useLayout(string $layoutFile, array $tplVars = []): void
{
$this->currentLayout = $layoutFile;
$this->layoutContent = $this->renderFile($layoutFile, $tplVars);
}

/**
* @param string $defaultLayout
*
Expand Down
1 change: 1 addition & 0 deletions src/PhpTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ protected function doRenderFile(string $tplFile, array $tplVars): string
}

ob_start();
/** @noinspection PhpRedundantOptionalArgumentInspection */
extract($tplVars, EXTR_OVERWRITE);
try {
require $tplFile;
Expand Down

0 comments on commit 3a85651

Please sign in to comment.