定制 dalpras/smart-template 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dalpras/smart-template

最新稳定版本:v1.5.0

Composer 安装命令:

composer require dalpras/smart-template

包简介

Smart template engine for key-value substitutions

README 文档

README

Introduction

Stop using template engines with new languages and new tricks to learn!
This is the right place: smart-template have the potential for building an engine based on callbacks that is lightning fast.

This is a full rendering system in a few lines of code.
smart-template library is designed for fast and efficient rendering without the mess of complicated libraries.

There are no dependencies!!

You will only use what you really need.

Features

  • key->value substitutions.
  • Nested templates files in a structered folders to access anything you need.
  • Deep nested rendering.
  • Customized attributes rendering for HTML elements.
  • Callbacks for customized rendering.
  • ...

Installation

As usual, composer make the job for you:

composer require dalpras/smart-template

Examples

Let's take a look to an example ...
Just some lines of code in different files.

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

echo $templateEngine->render('table.php', function ($render) {
        return $render['table']([
            '{class}' => 'text-end',
            '{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-end',
            '{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-end',
            '{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-end',
            '{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
];

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2023-10-09