greg-md/php-view

PHP 强大视图。

v1.0.0 2017-01-20 21:22 UTC

This package is auto-updated.

Last update: 2024-09-24 19:35:59 UTC


README

StyleCI Build Status Total Downloads Latest Stable Version Latest Unstable Version License

PHP 强大视图。

目录

要求

  • PHP 版本 ^7.1

编译器

  • PHP
  • Blade

工作原理

首先,您必须初始化一个 Viewer

$viewsDirectory = __DIR__ . '/views';

$viewer = new \Greg\View\Viewer($viewsDirectory);

可选,您可以添加一个视图编译器。例如一个为 Viewer 特别创建的 Blade Compiler

// Turn it to a callable, to load only when using blade templates.
$viewer->addExtension('.blade.php', function () {
    $compiledViewsDirectory = __DIR__ . '/compiled';

    return new \Greg\View\ViewBladeCompiler($compiledViewsDirectory);
});

默认情况下,它将使用 Renderer 作为模板的实例。

注意:如果您想使用自己的编译器,它必须是一个 编译策略 的实例。

现在,您可以在应用程序中的任何位置渲染视图。

在视图目录中创建一个模板文件。例如 welcome.blade.php

<html>
    <body>
        <h1>Hello, {{ $name }}</h1>
    </body>
</html>

在您的应用程序中使用 welcome 模板

$content = $viewer->render('welcome', [
    'name' => 'Greg',
]);

echo $content;

文档

许可

MIT © Grigorii Duca

长引用

I fear not the man who has practiced 10,000 programming languages once, but I fear the man who has practiced one programming language 10,000 times. #horrorsquad