dalpras/smart-template

键值替换的智能模板引擎

v1.4.1 2024-04-18 09:03 UTC

This package is auto-updated.

Last update: 2024-09-11 13:20:20 UTC


README

简介

停止使用新的语言和新技巧来学习的新模板引擎!
这是正确的位置:smart-template 有潜力构建一个基于回调的、闪电般的 引擎

这是几行代码的完整渲染系统。
smart-template 库是为了 快速高效 的渲染而设计的,避免了复杂库的 混乱

没有依赖项!!

你只会使用你真正需要的。

特性

  • 键值替换。
  • 在结构化文件夹中的嵌套模板文件以访问所需的一切。
  • 深度嵌套渲染。
  • 为HTML元素自定义属性渲染。
  • 为自定义渲染提供回调。
  • ...

安装

像往常一样,Composer 为您完成工作

composer require dalpras/smart-template

示例

让我们看一个例子...
不同文件中的几行代码。

/** ./mywebpage.php */
$templateEngine = new TemplateEngine(__DIR__ . '/templates');

echo $templateEngine->render('table.php', function ($render) {
        return $render['table']([
            '{class}' => 'text-right',
            '{cols}' => $render['row'](['{text}' => 'hello datum!'])
        ]);
    });

// or you can begin to nest everything you need

echo $templateEngine->render('table.php', function ($render) {
        return $render['table']([
            '{class}' => 'text-right',
            '{rows}' => function($render) {
                return $render['row'](['{text}' => 'hello nested!']);
            },
        ]);
    });


// or just use another file for rendering

echo $templateEngine->render('toolbar.php', function ($render) {
        return $render['header']([
            '{text}' => 'hello toolbar!'
        ]);
    });

// also you can use many files as you want

echo $templateEngine->render('table.php', function ($render) {
        return $render['table']([
            '{class}' => 'text-right',
            '{rows}' => function($render, TemplateEngine $template) {
                return $template->render('toolbar.php', fn($render) => $render['header']([
                    '{text}' => 'hello different template toolbar'
                ]));
            },
        ]);
    });

// or custom specific functions for rendering

echo $templateEngine->render('table.php', function ($render) {
        return $render['table']([
            '{class}' => 'text-right',
            '{rows}' => $render['myfunc']('hello func!'),
        ]);
    });


// GREAT POWER INVOLVES GREAT RESPONSIBILITY!
/** ./templates/table.php */
return [
    'table' => <<<html
        <div class="table-responsive">
            <table class="table table-sm {class}">
                <tbody>
                    {rows}
                </tbody>
            </table>
        </div>
        html,

    'row' => <<<html
        <tr><td>{text}</tr></td>
        html,
];
/** ./templates/nesteddir/toolbar.php */
return [
    'header' => <<<html
        <div class="toolbar">{text}</div>
        html,
        
    'myfunc' => fn($text) => 'This is your ' . $text
];