generated from inherelab/php-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update the simple template render logic and add tests
- Loading branch information
Showing
3 changed files
with
53 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PhpPkg\EasyTplTest; | ||
|
||
use PhpPkg\EasyTpl\SimpleTemplate; | ||
|
||
/** | ||
* class SimpleTemplateTest | ||
* | ||
* @author inhere | ||
* @date 2022/12/30 | ||
*/ | ||
class SimpleTemplateTest extends BaseTestCase | ||
{ | ||
public function testSimpleTemplate(): void | ||
{ | ||
$st = SimpleTemplate::new(); | ||
$st->setGlobalVars(['age' => 300]); | ||
|
||
$tpl = <<<TPL | ||
Hi, my name is {{ name }}, age is {{ age }}. | ||
first tag: {{ tags.0 }} | ||
user info: {{ user }} | ||
TPL; | ||
|
||
$vars = [ | ||
'name' => 'inhere', | ||
'tags' => ['php'], | ||
'user' => ['sex' => 'man'], | ||
]; | ||
$str = $st->renderString($tpl, $vars); | ||
$this->assertStringContainsString('name is inhere', $str); | ||
$this->assertStringContainsString('age is 300', $str); | ||
$this->assertStringContainsString('first tag: php', $str); | ||
$this->assertStringContainsString('user info: {sex: man}', $str); | ||
|
||
$str = $st->renderFile($this->getTestdataPath('simple.tpl'), $vars); | ||
$this->assertStringContainsString('name is inhere', $str); | ||
$this->assertStringContainsString('age is 300', $str); | ||
$this->assertStringContainsString('first tag: php', $str); | ||
$this->assertStringContainsString('user sex: man', $str); | ||
} | ||
} |
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,3 @@ | ||
Hi, my name is {{ name }}, age is {{ age }}. | ||
first tag: {{ tags.0 }} | ||
user sex: {{ user.sex }} |