1. Go to this page and download the library: Download pollen-solutions/view library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pollen-solutions / view example snippets
# /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;
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 . '!';
}
}
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;
}
}