Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

## 0.1.0 March 5, 2024

- Enh #2: Add helper `Template::class` (@terabytesoftw)
- Initial release
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,34 @@ $attributes = Attributes::render(
);
```

### Render Template

The `Template::class` helper can be used to render a template.

The method accepts two parameters:

- `template:` (string): The template to render.
- `tokenValues:` (array): The token values to replace in the template.

```php
<?php

declare(strict_types=1);

use PHPForge\Html\Helper\Template;

$template = '{{prefix}}\n{{tag}}\n{{suffix}}';
$tokenValues = [
'{{prefix}}' => 'prefix',
'{{tag}}' => '<div>content</div>',
'{{suffix}}' => 'suffix',
];

$content = Template::render($template, $tokenValues);
```

> `\n` is a new line character, and it is used to separate the tokens in the template.

### Validate value in list

The `Validator::class` helper can be used to validate a value in a list.
Expand Down
35 changes: 35 additions & 0 deletions src/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace UIAwesome\Html\Helper;

use function count;
use function explode;
use function strtr;

/**
* This class provides static methods for render template.
*/
final class Template
{
public static function render(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 . PHP_EOL : $result;
}
}

return $result;
}
}
48 changes: 48 additions & 0 deletions tests/TemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace UIAwesome\Html\Tests\Helper;

use PHPForge\Support\Assert;
use UIAwesome\Html\Helper\Template;

final class TemplateTest extends \PHPUnit\Framework\TestCase
{
public function testRender(): void
{
$template = '{{prefix}}\n{{tag}}\n{{suffix}}';
$tokenValues = [
'{{prefix}}' => 'prefix',
'{{tag}}' => '<div>content</div>',
'{{suffix}}' => '',
];

Assert::equalsWithoutLE(
<<<HTML
prefix
<div>content</div>
HTML,
Template::render($template, $tokenValues)
);
}

public function testRenderWithEmptyValuesTokens(): void
{
$template = '{{prefix}}\n{{tag}}\n{{suffix}}';
$tokenValues = [
'{{prefix}}' => '',
'{{label}}' => '',
'{{tag}}' => '<div>content</div>',
'{{suffix}}' => 'suffix',
];

Assert::equalsWithoutLE(
<<<HTML
<div>content</div>
suffix
HTML,
Template::render($template, $tokenValues)
);
}
}