pollen-solutions/view
Composer 安装命令:
composer require pollen-solutions/view
包简介
Pollen Solutions - View Component - View template engine system.
README 文档
README
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;
pollen-solutions/view 适用场景与选型建议
pollen-solutions/view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 569 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「view」 「template engine」 「component」 「pollen-solutions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pollen-solutions/view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pollen-solutions/view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pollen-solutions/view 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
A SilverStripe module to optimise the Meta, crawling, indexing, and sharing of your website content (forked from Cyber-Duck/Silverstripe-SEO)
A Processor for Markup written in PHP. Allows extraction of Markup into a data structure, orchestrated nested manipulation of said structure, and output as (optimized) Markup.
Illuminate View for Morningmedley.
Blade template for Kirby
Allow KoolReport to use twig template engine to render view
统计信息
- 总下载量: 569
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-09