57 lines
1.3 KiB
PHP
57 lines
1.3 KiB
PHP
|
<?php
|
||
|
namespace Framework\Renderer;
|
||
|
|
||
|
class TwigRenderer implements RendererInterface
|
||
|
{
|
||
|
|
||
|
private $twig;
|
||
|
|
||
|
public function __construct(\Twig_Environment $twig)
|
||
|
{
|
||
|
$this->twig = $twig;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Permet de rajouter un chamin pour charger les vues
|
||
|
* @param string $namespace
|
||
|
* @param null|string $path
|
||
|
*/
|
||
|
public function addPath(string $namespace, ?string $path = null): void
|
||
|
{
|
||
|
$this->twig->getLoader()->addPath($path, $namespace);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Permet de rendre une vue
|
||
|
* Le chemin peut être précisé avec des namespace rajoutés via addPath()
|
||
|
* $this->render('@blog/view');
|
||
|
* $this->render('view');
|
||
|
* @param string $view
|
||
|
* @param array $params
|
||
|
* @return string
|
||
|
*/
|
||
|
public function render(string $view, array $params = []): string
|
||
|
{
|
||
|
return $this->twig->render($view . '.twig', $params);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Permet de rajouter des variables globales à toutes les vues
|
||
|
*
|
||
|
* @param string $key
|
||
|
* @param mixed $value
|
||
|
*/
|
||
|
public function addGlobal(string $key, $value): void
|
||
|
{
|
||
|
$this->twig->addGlobal($key, $value);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return \Twig_Environment
|
||
|
*/
|
||
|
public function getTwig(): \Twig_Environment
|
||
|
{
|
||
|
return $this->twig;
|
||
|
}
|
||
|
}
|