bixev / light-html-template
Bixev 轻量级 HTML 模板系统
2.0.12
2021-11-28 20:12 UTC
Requires
- php: >=5.3
- bixev/light-logger: dev-master
Requires (Dev)
- phpunit/phpunit: ~8.0.4
README
这是一个简单的 HTML 模板系统,仅使用 HTML 模板,并使用少量的 PHP 命令。
它是脚本语言无关
和显示语言无关
的。您只需要了解3种结构
- block : 用于显示0次、1次或多次的代码片段
- var : 用于显示一个值(可选的过滤后)
- import : 用于从另一个文件导入模板
安装
建议使用 Composer 来安装此库。
composer require bixev/light-html-template "~1.0"
这将安装此库及其所有依赖项。
因此,您的每个 PHP 脚本都需要包含 composer 自动加载文件
<?php require 'vendor/autoload.php';
使用方法
HTML 模板
您的 .html
模板(或 string
)只能包含 HTML 和 <bloc>
/ {var}
以便工作。
您的 PHP 视图将处理条件、循环等
HTML 内容(在 .html 模板文件中,或从 PHP 的字符串 var 中)
Before first bloc {{var1 | unknownFunction}} {{ unknownVar }}{{unknownVar | unknownFunction}} {{bloc:myFirstBloc}} first bloc start {{var1}} {{bloc:mySecondBloc}} content of second bloc with {{var}} {{bloc:my5thBloc}} content of 5th bloc with {{ var }} {{endbloc:my5thBloc}} {{endbloc:mySecondBloc}} first bloc middle {{var3 }} {{ bloc:myThirdBloc }} content of third bloc with {{var}} {{endbloc:myThirdBloc}} {{bloc:my4thBloc}} content of 4th bloc with {{var}} {{ endbloc:my4thBloc }} first bloc end {{ var3}} {{endbloc:myFirstBloc}} After first bloc {{var1}}
PHP 内容(您的 PHP 视图)
<?php $tpl = \Bixev\LightHtmlTemplate\Factory::newTemplateFromString($template); $tpl->pB( [ 'var1' => 'testVal1', 'myFirstBloc' => [ 'var1' => 'testFirst1', 'var2' => 'testFirst2', 'var3' => 'testFirst3', 'mySecondBloc' => [ 'var' => 'testSecond', 'my5thBloc' => [ ['var' => 'test4th',], ['var' => 'test5th',], ], ], 'myThirdBloc' => [ ['var' => 'testThird1',], ['var' => 'testThird2',], ], ], ] ); $result = $tpl->render(); echo $result;
这将生成
Before first bloc testVal1
first bloc start testFirst1
content of second bloc with testSecond
content of 5th bloc with test4th
content of 5th bloc with test5th
first bloc middle testFirst3
content of third bloc with testThird1
content of third bloc with testThird2
first bloc end testFirst3
After first bloc testVal1
测试
vendor/bin/phpunit --bootstrap vendor/autoload.php ./test/BixevTest/LightHtmlTemplateTest/TplTest.php