generated from yii-tools/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ced2afa
commit 43df93d
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |