Final Class Yiisoft\View\Twig\TwigTemplateRenderer
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
TwigTemplateRenderer allows using Twig with a View service.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| render() | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $environment | \ |
|
public function __construct(private readonly Environment $environment) {}
| public string render ( \ | ||
| $view | \ |
|
| $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;
}
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.