//Zend\Expressive\Plates\PlatesRenderer
//....
public function getTemplate(){
if (null === $this->template) {
$this->template = $this->createTemplate();
}
return $this->template;
}
use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;
class ChangeCase implements ExtensionInterface
{
public function register(Engine $engine)
{
$engine->registerFunction('case', [$this, 'getObject']);
}
public function getObject()
{
return $this;
}
public function upper($var)
{
return strtoupper($var);
}
public function lower($var)
{
return strtolower($var);
}
}
//template.global.php
'templates' => [
'extension' => 'phtml',
'paths' => [
'app' => ['templates/app'],
'layout' => ['templates/layout'],
'error' => ['templates/error'],
'user' => ['templates/user'],
],
'plates_extensions' => [
Common\PlatesExtension\ChangeCase::class,
],
],
use Interop\Container\ContainerInterface;
use Zend\Expressive\Template\TemplateRendererInterface;
class LoadPlatesExtensionFactory
{
public function __invoke(ContainerInterface $container)
{
$template = $container->get(TemplateRendererInterface::class);
$config = $container->get('config') ? $container->get('config') : [];
$platesExtensions = [];
if (isset($config['templates']['plates_extensions'])) {
$platesExtensions = $config['templates']['plates_extensions'];
}
return new LoadPlatesExtension($template, $platesExtensions);
}
}
use League\Plates\Extension\ExtensionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Template\TemplateRendererInterface;
class LoadPlatesExtension
{
protected $platesEngine;
protected $platesExtensions;
public function __construct(TemplateRendererInterface $platesEngine, array $platesExtensions = [])
{
$this->platesEngine = $platesEngine;
$this->platesExtensions = $platesExtensions;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$this->_loadExtensions();
return $next($request, $response);
}
private function _loadExtensions(){
if(count($this->platesExtensions)>0){
foreach($this->platesExtensions as $extenstion) {
if($extenstion instanceof ExtensionInterface)
$this->platesEngine->loadExtension( new $extenstion() );
//after editing PlatesRenderer class it works
//$this->platesEngine->getTemplate()->loadExtension(new $extension());
else
throw \Exception(sprintf("%s not implement %s", $extenstion, ExtensionInterface::class));
}
}
}
}