irfantoor / template-engine
一个简单、小巧且智能的模板引擎
0.3.3
2023-02-06 18:12 UTC
Requires
- php: >= 7.3
Requires (Dev)
- irfantoor/test: ~0.7
README
一个简单且小巧的模板引擎。
快速开始
安装
安装或在项目中包含
$ composer require irfantoor/template-engine
测试模板引擎
$ vendor/bin/test
创建模板引擎
$te = new IrfanTOOR\TemplateEngine([ 'max_depth' => 3, # defaults to 3 'base_path' => 'your/path/to/template/files/', ]);
处理文本
$text = "{$greeting} {$user}!"; $data = [ 'greeting' => 'Hello', 'user' => 'World', ]; echo $te->processText($text, $data);
处理文件
# home.php <h1>{$title}</h1> <ul> {@foreach ($list as $item):} <li>{$item}</li> {@endforeach} </ul>
$data = [ 'title' => 'Fruits', 'list' => [ 'Apple', 'Orange', 'Blackberry', 'Raspberry', ], ]; echo $te->processFile("home.php", $data);
模板
注释
格式: {#...}
{# its a comment!} {#anything here including the external brackets are removed from the output}
标记
格式: {$...}
标记会被替换为通过传入的数据数组提供的值。
{$name['first']} {$name['last']}
{$address[0]}
{$address[1]}
tel: {$tel}
email: {$email}
格式: {!$...} 标记会被替换为标签,而不会被进行任何HTML特殊字符转换。这有助于包含HTML标签等,它们会被显示为HTML而不是内容。
命令
格式: {@...}
{@include 'header.php'} {@echo date('d-m-Y')} {@if ($list):} # Note: you can use the curley brackets, so use the form foreach (...): endforeach instead {@foreach ($list as $k => $v):} data provided is : {$k} | {$v} {@endforeach} {@endif} # you can define the data in the template {@ $d = date('d-m-Y')} # and prints ... date : {$d} # Note: The statement in {@ ...} tags need not to be terminated with a semicolon ';' {@ $list = [ 'black', 'white' ]} dump list: # Note: The variable to dump, might as well be an object, array, bool int or a string {$list}
注意:在 {@ 之后,您可以使用命令,就像您在使用PHP代码一样,尽管只有的一个约束是不能使用循环或使用花括号的命令。