Skip to content

Commit

Permalink
refactor: update the simple template logic, support filters
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 15, 2023
1 parent fde8adc commit 030a5bd
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/Concern/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public static function new(array $config = []): static
public function __construct(array $config = [])
{
Obj::init($this, $config);

$this->afterInit();
}

protected function afterInit(): void
{
// do something
}

/**
Expand Down
111 changes: 103 additions & 8 deletions src/SimpleTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@

use InvalidArgumentException;
use PhpPkg\EasyTpl\Concern\AbstractTemplate;
use Toolkit\Stdlib\Arr;
use Toolkit\Stdlib\Std\PipeFilters;
use Toolkit\Stdlib\Str;
use function array_merge;
use function explode;
use function file_get_contents;
use function is_array;
use function preg_quote;
use function preg_replace_callback;
use function sprintf;
use function str_contains;
use function trim;

/**
* Class SimpleTemplate
Expand All @@ -37,11 +44,38 @@ class SimpleTemplate extends AbstractTemplate
*
* @var string
*/
private string $varLeft = '{{';
private string $varStart = '{{';

/**
* regex pattern, build from $format
*
* @var string
*/
private string $pattern = '';

/**
* @var PipeFilters|null
*/
private ?PipeFilters $pipeFilter = null;

/**
* @return void
*/
protected function afterInit(): void
{
$this->pipeFilter = PipeFilters::newWithDefaultFilters();

// add built in filters
$this->pipeFilter->addFilters([
'nl' => function ($val) {
return $val . "\n";
}
]);
}

/**
* @param string $tplFile
* @param array $tplVars
* @param array $tplVars
*
* @return string
*/
Expand All @@ -54,21 +88,62 @@ public function renderFile(string $tplFile, array $tplVars = []): string

/**
* @param string $tplCode
* @param array $tplVars
* @param array $tplVars
*
* @return string
*/
public function renderString(string $tplCode, array $tplVars = []): string
{
if (!str_contains($tplCode, $this->varLeft)) {
if (!str_contains($tplCode, $this->varStart)) {
return $tplCode;
}

if ($this->globalVars) {
$tplVars = array_merge($this->globalVars, $tplVars);
}

return Str::renderVars($tplCode, $tplVars, $this->format);
return $this->renderVars($tplCode, $tplVars);
}

/**
* @param string $tplCode
* @param array $vars
*
* @return string
*/
protected function renderVars(string $tplCode, array $vars): string
{
$fmtVars = Arr::flattenMap($vars, Arr\ArrConst::FLAT_DOT_JOIN_INDEX);

// convert array value to string.
foreach ($vars as $name => $val) {
if (is_array($val)) {
$fmtVars[$name] = Arr::toStringV2($val);
}
}

if (!$this->pattern) {
$this->parseFormat($this->format);
}

return preg_replace_callback($this->pattern, function (array $match) use ($fmtVars) {
$var = trim($match[1]);
if (!$var) {
return $match[0];
}

$filters = '';
if (str_contains($var, '|')) {
[$var, $filters] = Str::explode($var, '|', 2);
}

if (isset($fmtVars[$var])) {
$val = $fmtVars[$var];
return $filters ? $this->pipeFilter->applyStringRules($val, $filters) : $val;
}

return $match[0];
}, $tplCode);
}

/**
Expand All @@ -89,8 +164,28 @@ public function setFormat(string $format): void
}

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

/**
* @return PipeFilters
*/
public function getPipeFilter(): PipeFilters
{
return $this->pipeFilter;
}

/**
* @param string $format
*
* @return void
*/
protected function parseFormat(string $format): void
{
[$left, $right] = explode('%s', $format);

$this->varStart = $left;
$this->pattern = sprintf('/%s([^\n]+?)%s/', preg_quote($left, '/'), preg_quote($right, '/'));
}

}
24 changes: 24 additions & 0 deletions test/SimpleTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,28 @@ public function testSimpleTemplate(): void
$this->assertStringContainsString('first tag: php', $str);
$this->assertStringContainsString('user sex: man', $str);
}

public function testSimpleTemplate_withFilters(): void
{
$st = SimpleTemplate::new();
$vs = [
'age' => 300,
'name' => 'inhere',
'tags' => ['php'],
'user' => ['sex' => 'man'],
];

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

$str = $st->renderString($tpl, $vs);
$this->assertStringContainsString('name is INH', $str);
$this->assertStringContainsString('age is 300', $str);
$this->assertStringContainsString('first tag: PHP', $str);
$this->assertStringContainsString('user info: {sex: man}', $str);

}
}

0 comments on commit 030a5bd

Please sign in to comment.