From 1ad46170ce938ed709ac6fd05cd8ddbadac5e038 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 5 Mar 2024 11:51:27 -0300 Subject: [PATCH 1/4] Add helper `Template::class`. --- README.md | 28 ++++++++++++++++++++++++ src/Template.php | 35 ++++++++++++++++++++++++++++++ tests/TemplateTest.php | 48 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 src/Template.php create mode 100644 tests/TemplateTest.php diff --git a/README.md b/README.md index e4554e2..dffe969 100644 --- a/README.md +++ b/README.md @@ -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 + 'prefix', + '{{tag}}' => '
content
', + '{{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. diff --git a/src/Template.php b/src/Template.php new file mode 100644 index 0000000..675a1e0 --- /dev/null +++ b/src/Template.php @@ -0,0 +1,35 @@ + $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; + } +} diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php new file mode 100644 index 0000000..db07139 --- /dev/null +++ b/tests/TemplateTest.php @@ -0,0 +1,48 @@ + 'prefix', + '{{tag}}' => '
content
', + '{{suffix}}' => '', + ]; + + Assert::equalsWithoutLE( + <<content + HTML, + Template::render($template, $tokenValues) + ); + } + + public function testRenderWithEmptyValuesTokens(): void + { + $template = '{{prefix}}\n{{tag}}\n{{suffix}}'; + $tokenValues = [ + '{{prefix}}' => '', + '{{label}}' => '', + '{{tag}}' => '
content
', + '{{suffix}}' => 'suffix', + ]; + + Assert::equalsWithoutLE( + <<content + suffix + HTML, + Template::render($template, $tokenValues) + ); + } +} From a5499fc9980c46428f3e7806d023722196732b80 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 5 Mar 2024 11:54:47 -0300 Subject: [PATCH 2/4] Add line `CHANGELOG.md`. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e22acd..984d64a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,4 +4,5 @@ ## 0.1.0 March 5, 2024 +- Enh #2: Add helper `Template::class` (@terabytesoftw) - Initial release From f4a97a798cecbaadbbf7a85f731c024b9d72e4c1 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 5 Mar 2024 11:57:58 -0300 Subject: [PATCH 3/4] Fix `README.md`. --- CHANGELOG.md | 1 + README.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 984d64a..86c1a6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,5 +4,6 @@ ## 0.1.0 March 5, 2024 +- Bug #3: Fix `README.md` (@terabytesoftw) - Enh #2: Add helper `Template::class` (@terabytesoftw) - Initial release diff --git a/README.md b/README.md index dffe969..46cac3e 100644 --- a/README.md +++ b/README.md @@ -279,7 +279,7 @@ The method accepts two parameters: declare(strict_types=1); -use PHPForge\Html\Helper\Template; +use UIAwesome\Html\Helper\Template; $template = '{{prefix}}\n{{tag}}\n{{suffix}}'; $tokenValues = [ From e181cfa0b89f6428902b7444015c5eb87bb38864 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 5 Mar 2024 12:00:00 -0300 Subject: [PATCH 4/4] Fix error typo. --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 0d71f8f..46cac3e 100644 --- a/README.md +++ b/README.md @@ -404,4 +404,3 @@ The MIT License (MIT). Please see [License File](LICENSE) for more information. ## Our social networks [![Twitter](https://img.shields.io/badge/twitter-follow-1DA1F2?logo=twitter&logoColor=1DA1F2&labelColor=555555?style=flat)](https://twitter.com/Terabytesoftw) -