Skip to content

Commit

Permalink
Add Template::class helper. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Mar 3, 2024
1 parent ced2afa commit 43df93d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Bug #9: Add 'formaction' to list of evil attributes to remove in `Sanitize::class` helper (@terabytesoftw)
- Bug #10: Update `phpdoc` in helper classes (@terabytesoftw)
- Enh #11: Add `Validator::class` helper (@terabytesoftw)
- Enh #12: Add `Template::class` helper (@terabytesoftw)

## 0.2.1 March 2, 2024

Expand Down
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 PHPForge\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 PHPForge\Html\Tests\Helper;

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

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)
);
}
}

0 comments on commit 43df93d

Please sign in to comment.