花粉解决方案 - 视图组件 - 视图模板引擎系统。

v1.0.3 2021-07-09 00:00 UTC

This package is auto-updated.

Last update: 2024-09-30 01:52:27 UTC


README

Latest Version MIT Licensed PHP Supported Versions

花粉解决方案 View 组件是一个模板引擎系统。

这是一个可扩展的显示模板引擎,它原生集成了 PlatesTwig 库。

安装

composer require pollen-solutions/view

基础

关于 Plates

Plates 是一个快速、易于使用的原生 PHP 模板系统。它受到了 Twig 模板引擎的启发。Plates 旨在为那些更喜欢使用原生 PHP 模板而不是编译模板语言的开发者设计。

Plates 是花粉解决方案组件套件中的默认引擎。

更多信息

关于 Twig

Twig 是包含在 Symfony 框架 中的模板引擎。 Twig 是一个用于 PHP 的新颖模板引擎。 Twig 将模板编译成优化的纯 PHP 代码。

Twig 已原生包含在花粉视图组件中。

更多信息

第三方引擎

Blade

Blade 是包含在 Laravel 框架 中的模板引擎。

Blade 并未原生包含在花粉视图组件中,但可以轻松添加。

composer require pollen-solutions/view-blade

更多信息

Mustache

Mustache PHP 引擎目前正在项目中,即将推出。

更多信息

基础

统一的 API 接口

为了响应每个模型显示引擎的特定性,花粉视图利用了一个统一的接口,这使得可以通过相同的 API 使用不同的引擎。

目录和覆盖

花粉 View 使用了与它继承的库不同的逻辑。

在模板目录中包含的每个模板文件都可以被覆盖目录中同名的模板文件替换。

扩展模板引擎

花粉 View 还通过一个简单的接口使得扩展模板显示引擎的功能成为可能。

缓存

为了便于应用开发,花粉视图允许您禁用它所实现的显示模板引擎的缓存。

强烈建议在生产部署时启用缓存。

使用全局视图

模板文件

# /var/www/html/views/hello-world.plates.php
echo 'Hello World !';

视图调用

use Pollen\View\ViewManager;

$viewManager = new ViewManager();

$viewManager->setDirectory('/var/www/html/views');

echo $view->render('hello-world');
exit;

创建新的视图实例

简单用法

模板文件

# /var/www/html/views/hello-world.plates.php
echo 'Hello World' . $this->get('name') . '!';

视图调用

use Pollen\View\ViewManager;

$viewManager = new ViewManager();

## Creating a Plates View
$view = $viewManager->createView('plates')->setDirectory('/var/www/html/views');

echo $view->render('hello-world', ['name' => 'John Doe']);
exit;

高级用法

在这个例子中,我们使用自定义模板类,并通过视图引擎回调配置视图。

自定义模板类

namespace Acme\View;

use Pollen\View\Engines\Plates\PlatesTemplate as BasePlatesTemplate;

class PlatesTemplate extends BasePlatesTemplate
{
    public function helloWorldName(string $name): string
    {
        return 'Hello World '. $name . '!';
    }
}

模板文件

# /var/www/html/views/hello-world.plates.php
/**
 * @var Acme\View\PlatesTemplate $this
 */
echo $this->helloWorldName($this->get('name'));

视图调用

use Pollen\View\ViewManager;
use Pollen\View\Engines\Plates\PlatesViewEngine;
use Acme\View\PlatesTemplate;

$viewManager = new ViewManager();

$directory = '/var/www/html/views';

$view = $viewManager->createView(
    (new PlatesViewEngine()),
    function (PlatesViewEngine $platesViewEngine) use ($directory) {
        $platesViewEngine->platesEngine()
            ->setDirectory($directory);

        $platesViewEngine->platesEngine()->setTemplateClass(PlatesTemplate::class);

        return $platesViewEngine;
    }
);

echo $view->render('hello-world', ['name' => 'John Doe']);
exit;

扩展视图

简单方法(使用回调)

模板文件

# /var/www/html/views/hello-world.plates.php
echo $this->helloWorldName($this->get('name'));

视图调用

use Pollen\View\ViewManager;

$viewManager = new ViewManager();

$view = $viewManager->createView('plates')
    ->setDirectory('/var/www/html/views')
    ->addExtension('helloWorldName', function (string $name): string {
        return sprinf('Hello World %s !', $name);
    });

echo $view->render('hello-world', ['name' => 'John Doe']);
exit;

使用视图扩展类的高级方法

视图扩展类

use Acme\View;

use Pollen\View\ViewExtension;
use Pollen\View\ViewEngineInterface;
use Pollen\View\Engines\Plates\PlatesViewEngine;
use Pollen\View\Engines\Twig\TwigViewEngine;
use Twig\TwigFunction;

class HelloWorldNameViewExtension extends ViewExtension
{
    public function register(ViewEngineInterface $viewEngine)
    {
        if (is_a($viewEngine, PlatesViewEngine::class)) {
            $viewEngine->platesEngine()->registerFunction(
                    $this->getName(),
                    function (string $name): string {
                        return sprinf('Hello World %s !', $name);
                    }
                );
        }
        
        /**
         * Extending Twig
         * @see https://twig.symfony.com.cn/doc/3.x/advanced.html 
         */
        if (is_a($viewEngine, TwigViewEngine::class)) {
            $viewEngine->twigEnvironment()->addFunction(
                new TwigFunction(
                    $this->getName(),
                    function (string $name): string {
                        return sprinf('Hello World %s !', $name);
                    }
                )
            );
        }
           
        return null;
    }
}

模板文件

# /var/www/html/views/hello-world.plates.php
echo $this->helloWorldName($this->get('name'));

视图调用

use Pollen\View\ViewManager;
use Acme\View\HelloWorldNameViewExtension;

$viewManager = new ViewManager();

$view = $viewManager->createView('plates')
    ->setDirectory('/var/www/html/views')
    ->addExtension('helloWorldName', new HelloWorldNameViewExtension());

echo $view->render('hello-world', ['name' => 'John Doe']);
exit;