XBackBone/app/Web/View.php

71 lines
1.4 KiB
PHP
Raw Normal View History

2019-11-12 23:13:23 +00:00
<?php
namespace App\Web;
use DI\Container;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Factory\ServerRequestCreatorFactory;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
class View
{
/**
* @var Environment
*/
private $twig;
/**
* View constructor.
2019-11-13 12:02:31 +00:00
* @param Environment $twig
2019-11-12 23:13:23 +00:00
*/
2019-11-13 12:02:31 +00:00
public function __construct(Environment $twig)
2019-11-12 23:13:23 +00:00
{
$this->twig = $twig;
}
/**
* @param Response $response
* @param string $view
* @param array|null $parameters
* @return Response
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function render(Response $response, string $view, ?array $parameters = [])
{
$body = $this->twig->render($view, $parameters);
$response->getBody()->write($body);
return $response;
}
/**
* @param string $view
* @param array|null $parameters
* @return string
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function string(string $view, ?array $parameters = [])
{
return $this->twig->render($view, $parameters);
}
/**
* @return Environment
*/
public function getTwig(): Environment
{
return $this->twig;
}
2019-11-12 23:13:23 +00:00
}