srcoder/template-bridge

在您的旧模板引擎与其他模板引擎之间建立桥梁

1.0.2 2017-12-13 16:45 UTC

This package is auto-updated.

Last update: 2024-09-20 21:48:17 UTC


README

在您的旧 PHP 代码和新版模板引擎之间建立桥梁。

依赖项

需要至少 PHP 7.0。还使用 srcoder/normalize-strings 进行字符串操作。

引擎

  • 纯模板
  • Twig
  • 兼容

基本用法

使用管理器定义您的模板。

use \Srcoder\TemplateBridge\Manager;
use \Srcoder\TemplateBridge\Engine\Twig;
use \Srcoder\TemplateBridge\Engine\Plain;

// Initialize template bridge
$templateBridge = new Manager;
// or static
$templateBridge = Manager::instance();

// Add a engine
$templateBridge->add(
        'twig',         // Name
        new Twig(),     // Template engine
        300             // Prio, higher is more important
);

$templateBridge->add(
        'plain',        // Name
        new Plain(),    // Template engine
        600             // Prio, higher is more important
);


// Adding a file
$templateManager->addFile('file');
// Will search for file.twig and file

// Render template
echo $templateBridge->render();

数据

在渲染内容时,您可以向其添加数据。

use \Srcoder\TemplateBridge\Data;

echo $templateBridge->render(new Data(['key' => 'value']))

有趣的部分

Twig 引擎

当找到 Twig 文件时,它将按您预期的方式工作。

纯模板引擎

纯模板文件将替换数据对象中的 {{$variable}}。纯模板引擎也可以用于 JavaScript 和 HTML。

兼容引擎