pollen-solutions/view 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

pollen-solutions/view

Composer 安装命令:

composer require pollen-solutions/view

包简介

Pollen Solutions - View Component - View template engine system.

README 文档

README

Latest Version MIT Licensed PHP Supported Versions

Pollen Solutions View Component is a template engine system.

This is an expandable display template engine that natively integrates Plates and Twig library.

Installation

composer require pollen-solutions/view

Fundamentals

About Plates

Plates is a native PHP template system that’s fast, easy to use. It’s inspired by the Twig template engine. Plates is designed for developers who prefer to use native PHP templates over compiled template languages.

Plates is use as default engine in the Pollen Solutions Components Suite.

More informations :

About Twig

Twig is the templating engine that included with Symfony Framework. Twig is a modern template engine for PHP. Twig compiles templates down to plain optimized PHP code.

Twig is natively included with Pollen View component.

More informations :

Third-party Engine

Blade

Blade is the templating engine that included with Laravel Framework.

Blade is not natively included with the Pollen View component, but can easily be added :

composer require pollen-solutions/view-blade

More informations :

Mustache

Mustache PHP engine is currently in project and coming soon.

More informations :

Fundamentals

An unified API interface

To respond to the particularity of each of the model display engines, Pollen View benefits from a unified interface this makes it possible to work with different engines via the same API.

Directory and override

Pollen View purposes a different logic from the libraries it inherits.

Each template file included in the template directory can be replaced by a template file with the same name in the override directory.

Extending the template engine

Pollen View also makes it possible to extend the functionalities of the template display engines through an easy interface.

Caching

To facilitate the work of application development, Pollen View allows you to disable the cache of the display template engines that it implements.

It is strongly recommended that you enable the cache when deploying to production.

Using global View

Template file

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

View call

use Pollen\View\ViewManager;

$viewManager = new ViewManager();

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

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

Creating a new view instance

Simple usage

Template file

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

View call

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;

Advanced usage

In this example we use a customized template class and the view is configured through the view engine callback.

Customized template class

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 . '!';
    }
}

Template file

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

View call

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;

Extending a View

Simple method (with a callback)

Template file

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

View call

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;

Advanced method with View Extension class

View Extension class

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/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;
    }
}

Template file

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

View call

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;

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固