0 follower

Final Class Yiisoft\View\Twig\TwigTemplateRenderer

InheritanceYiisoft\View\Twig\TwigTemplateRenderer
ImplementsYiisoft\View\TemplateRendererInterface

TwigTemplateRenderer allows using Twig with a View service.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Twig\Environment $environment )
$environment \Twig\Environment

                public function __construct(private readonly Environment $environment)
{
}

            
render() public method

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;
    }
}