Final Class Yiisoft\View\Twig\TwigTemplateRenderer
| Inheritance | Yiisoft\View\Twig\TwigTemplateRenderer |
|---|---|
| Implements | Yiisoft\View\TemplateRendererInterface |
TwigTemplateRenderer allows using Twig with a View service.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\View\Twig\TwigTemplateRenderer | |
| render() | Yiisoft\View\Twig\TwigTemplateRenderer |
Method Details
| public mixed __construct ( \Twig\Environment $environment ) | ||
| $environment | \Twig\Environment | |
public function __construct(private readonly Environment $environment)
{
}
| public string render ( \Yiisoft\View\ViewInterface $view, string $template, array $parameters ) | ||
| $view | \Yiisoft\View\ViewInterface | |
| $template | string | |
| $parameters | array | |
public function render(ViewInterface $view, string $template, array $parameters): string
{
$templateFile = str_replace(
$view->getBasePath(),
'',
$template
);
$obInitialLevel = ob_get_level();
ob_start();
ob_implicit_flush(false);
try {
echo $this->environment->render($templateFile, array_merge($parameters, ['this' => $view]));
/**
* @var string We assume that in this case active output buffer is always existed, so `ob_get_clean()`
* returns a string.
*/
return ob_get_clean();
} catch (Throwable $e) {
while (ob_get_level() > $obInitialLevel) {
ob_end_clean();
}
throw $e;
}
}
Signup or Login in order to comment.