File tree Expand file tree Collapse file tree 4 files changed +112
-0
lines changed Expand file tree Collapse file tree 4 files changed +112
-0
lines changed Original file line number Diff line number Diff line change 44
55## 0.1.0 March 5, 2024
66
7+ - Enh #2 : Add helper ` Template::class ` (@terabytesoftw )
78- Initial release
Original file line number Diff line number Diff line change @@ -265,6 +265,34 @@ $attributes = Attributes::render(
265265);
266266```
267267
268+ ### Render Template
269+
270+ The ` Template::class ` helper can be used to render a template.
271+
272+ The method accepts two parameters:
273+
274+ - ` template: ` (string): The template to render.
275+ - ` tokenValues: ` (array): The token values to replace in the template.
276+
277+ ``` php
278+ <?php
279+
280+ declare(strict_types=1);
281+
282+ use PHPForge\Html\Helper\Template;
283+
284+ $template = '{{prefix}}\n{{tag}}\n{{suffix}}';
285+ $tokenValues = [
286+ '{{prefix}}' => 'prefix',
287+ '{{tag}}' => '<div >content</div >',
288+ '{{suffix}}' => 'suffix',
289+ ];
290+
291+ $content = Template::render($template, $tokenValues);
292+ ```
293+
294+ > ` \n ` is a new line character, and it is used to separate the tokens in the template.
295+
268296### Validate value in list
269297
270298The ` Validator::class ` helper can be used to validate a value in a list.
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace UIAwesome \Html \Helper ;
6+
7+ use function count ;
8+ use function explode ;
9+ use function strtr ;
10+
11+ /**
12+ * This class provides static methods for render template.
13+ */
14+ final class Template
15+ {
16+ public static function render (string $ template , array $ tokenValues ): string
17+ {
18+ $ result = '' ;
19+ $ tokens = explode ('\n ' , $ template );
20+
21+ foreach ($ tokens as $ key => $ token ) {
22+ $ tokenValue = strtr ($ token , $ tokenValues );
23+
24+ if ($ tokenValue !== '' ) {
25+ $ result .= $ tokenValue ;
26+ }
27+
28+ if ($ result !== '' && $ key < count ($ tokens ) - 1 ) {
29+ $ result = strtr ($ tokens [$ key + 1 ], $ tokenValues ) !== '' ? $ result . PHP_EOL : $ result ;
30+ }
31+ }
32+
33+ return $ result ;
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace UIAwesome \Html \Tests \Helper ;
6+
7+ use PHPForge \Support \Assert ;
8+ use UIAwesome \Html \Helper \Template ;
9+
10+ final class TemplateTest extends \PHPUnit \Framework \TestCase
11+ {
12+ public function testRender (): void
13+ {
14+ $ template = '{{prefix}}\n{{tag}}\n{{suffix}} ' ;
15+ $ tokenValues = [
16+ '{{prefix}} ' => 'prefix ' ,
17+ '{{tag}} ' => '<div>content</div> ' ,
18+ '{{suffix}} ' => '' ,
19+ ];
20+
21+ Assert::equalsWithoutLE (
22+ <<<HTML
23+ prefix
24+ <div>content</div>
25+ HTML ,
26+ Template::render ($ template , $ tokenValues )
27+ );
28+ }
29+
30+ public function testRenderWithEmptyValuesTokens (): void
31+ {
32+ $ template = '{{prefix}}\n{{tag}}\n{{suffix}} ' ;
33+ $ tokenValues = [
34+ '{{prefix}} ' => '' ,
35+ '{{label}} ' => '' ,
36+ '{{tag}} ' => '<div>content</div> ' ,
37+ '{{suffix}} ' => 'suffix ' ,
38+ ];
39+
40+ Assert::equalsWithoutLE (
41+ <<<HTML
42+ <div>content</div>
43+ suffix
44+ HTML ,
45+ Template::render ($ template , $ tokenValues )
46+ );
47+ }
48+ }
You can’t perform that action at this time.
0 commit comments