zvax/templating

一个简单的 PHP 模板解析器

2.0.0 2023-01-19 07:32 UTC

This package is auto-updated.

Last update: 2024-09-20 01:44:06 UTC


README

大部分是废弃的项目,用于渲染接口和 Twig 适配器,以便我在其他项目中使用。

以下内容仍然有一定的真实性。

简单的模板解析引擎

引擎是解析器,它将正则表达式模板字符串并替换键为值

$templateString = '{first}{$second}{$obj->third}';
$engine = new Engine();
$obj = new stdClass();
$obj->third = 'sentence.';
$string = $engine->render($templateString,[
	'first' => 'This ',
	'second' => 'is a ',
	'obj' => $obj,
]);
// This is a sentence.

有非常基本的循环支持

$string = $engine->render('{foreach $posts as $post}{$post}{/foreach}',[
            'posts' => [
                'post1',
                'post2',
            ],
        ]);
// post1post2

posts 数组必须是一个 0 索引的字符串容器