Pollen Solutions View Component is a template engine system.
This is an expandable display template engine that natively integrates Plates and Twig library.
composer require pollen-solutions/view
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 :
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 :
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 PHP engine is currently in project and coming soon.
More informations :
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.
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.
Pollen View also makes it possible to extend the functionalities of the template display engines through an easy interface.
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.
# /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;
In this example we use a customized template class and the view is configured through the view engine callback.
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/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;