Skip to content

Commit

Permalink
Refactor template rendering logic in several classes. (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 29, 2023
1 parent b87f510 commit 728d15f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 57 deletions.
22 changes: 22 additions & 0 deletions src/Attribute/Custom/HasTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace PHPForge\Html\Attribute\Custom;

use function explode;

/**
* Is used by widgets that implement the template method.
*/
Expand Down Expand Up @@ -33,4 +35,24 @@ public function tokenValue(string $token, string $value): static

return $new;
}

private function renderTemplate(string $template, array $tokenValues): string
{
$result = '';
$tokens = explode('\n', $template);

foreach ($tokens as $key => $token) {
$tokenValue = strtr($token, $tokenValues);

if ($tokenValue !== '') {
$result .= $tokenValue;
}

if ($result !== '' && $key < count($tokens) - 1) {
$result = strtr($tokens[$key + 1], $tokenValues) !== '' ? $result . "\n" : $result;
}
}

return $result;
}
}
17 changes: 1 addition & 16 deletions src/Base/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,13 @@ protected function run(): string
$attributes['id'] = $this->id;
}

$result = '';
$tokenValues = [
'{prefix}' => $this->renderPrefixTag(),
'{tag}' => HtmlBuilder::create($this->tagName, $this->content, $attributes),
'{suffix}' => $this->renderSuffixTag(),
];
$tokenValues += $this->tokenValue;

$tokens = explode('\n', $this->template);

foreach ($tokens as $key => $token) {
$tokenValue = strtr($token, $tokenValues);

if ($tokenValue !== '') {
$result .= $tokenValue;
}

if ($result !== '' && isset($tokens[$key + 1])) {
$result = strtr($tokens[$key + 1], $tokenValues) !== '' ? $result . "\n" : $result;
}
}

return $result;
return $this->renderTemplate($this->template, $tokenValues);
}
}
14 changes: 2 additions & 12 deletions src/ButtonToggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,15 @@ protected function run(): string
$attributes['data-toggle'] = $this->toggleId;
}

$result = '';
$tokenValues = [
'{toggle}' => $this->renderToggleTag(),
'{icon}' => $this->renderIconTag(),
'{content}' => $this->content,
];
$tokenValues += $this->tokenValue;

$tokens = explode('\n', $this->template);

foreach ($tokens as $token) {
$tokenValue = strtr($token, $tokenValues);

if ($tokenValue !== '') {
$result .= $tokenValue . "\n";
}
}

$content = $result !== '' ? PHP_EOL . $result : '';
$content = $this->renderTemplate($this->template, $tokenValues);
$content = $content !== '' ? PHP_EOL . $content . PHP_EOL : '';

return Button::widget()
->ariaDescribedBy($this->ariaDescribedBy)
Expand Down
43 changes: 14 additions & 29 deletions src/Input/Base/AbstractInputChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,24 @@ protected function buildChoiceTag(string $type): string
$labelTag = $this->renderLabelTag($this->getId() ?? '');
}

$choiceTag = $this->renderTemplate($tag, $labelTag);
$choiceTag = $this->prepareTemplate($tag, $labelTag);

return $this->renderContainerTag($choiceTag);
}

private function prepareTemplate(string $tag, string $labelTag): string
{
$tokenValues = [
'{prefix}' => $this->renderPrefixTag(),
'{unchecktag}' => $this->renderUncheckTag(),
'{tag}' => $tag,
'{label}' => $labelTag,
'{suffix}' => $this->renderSuffixTag(),
];

return $this->renderTemplate($this->template, $tokenValues);
}

private function renderEnclosedByLabel(string $tag): string
{
if ($this->labelContent === '' || $this->isNotLabel()) {
Expand All @@ -115,32 +128,4 @@ private function renderEnclosedByLabel(string $tag): string
->for($this->getId())
->render();
}

private function renderTemplate(string $tag, string $labelTag): string
{
$result = '';
$tokenValues = [
'{prefix}' => $this->renderPrefixTag(),
'{unchecktag}' => $this->renderUncheckTag(),
'{tag}' => $tag,
'{label}' => $labelTag,
'{suffix}' => $this->renderSuffixTag(),
];

$tokens = explode('\n', $this->template);

foreach ($tokens as $key => $token) {
$tokenValue = strtr($token, $tokenValues);

if ($tokenValue !== '') {
$result .= $tokenValue;
}

if ($result !== '' && isset($tokens[$key + 1])) {
$result = strtr($tokens[$key + 1], $tokenValues) !== '' ? $result . "\n" : $result;
}
}

return $result;
}
}

0 comments on commit 728d15f

Please sign in to comment.