diff --git a/src/PhpTemplateRenderer.php b/src/PhpTemplateRenderer.php index 16786add1..08cfb2e8c 100644 --- a/src/PhpTemplateRenderer.php +++ b/src/PhpTemplateRenderer.php @@ -19,7 +19,7 @@ */ final class PhpTemplateRenderer implements TemplateRendererInterface { - public function render(ViewInterface $view, string $template, array $parameters): string + public function render(Template $template): string { $renderer = function (): void { /** @psalm-suppress MixedArgument */ @@ -33,7 +33,7 @@ public function render(ViewInterface $view, string $template, array $parameters) ob_implicit_flush(false); try { /** @psalm-suppress PossiblyInvalidFunctionCall,PossiblyNullFunctionCall */ - $renderer->bindTo($view)($template, $parameters); + $renderer->bindTo($template->getView())($template->getPath(), $template->getParameters()); return ob_get_clean(); } catch (Throwable $e) { while (ob_get_level() > $obInitialLevel) { diff --git a/src/Template.php b/src/Template.php new file mode 100644 index 000000000..dc9778bc1 --- /dev/null +++ b/src/Template.php @@ -0,0 +1,65 @@ +path; + } + + /** + * Get the parameters to pass to the template. + * + * @return array The parameters to pass to the template. + */ + public function getParameters(): array + { + return $this->parameters; + } + + /** + * Get the view instance used for rendering the file. + * + * @return ViewInterface The view instance used for rendering the file. + */ + public function getView(): ViewInterface + { + return $this->view; + } + + /** + * Get the context instance of the view. + * + * @return ViewContextInterface|null The context instance of the view. + */ + public function getViewContext(): ?ViewContextInterface + { + return $this->viewContext; + } +} diff --git a/src/TemplateRendererInterface.php b/src/TemplateRendererInterface.php index 9b17b85b9..2f6f9f977 100644 --- a/src/TemplateRendererInterface.php +++ b/src/TemplateRendererInterface.php @@ -15,11 +15,7 @@ interface TemplateRendererInterface * This method is invoked by {@see View} and {@see WebView} whenever it tries to render a view. * The classes must implement this method to render the given view file. * - * @param ViewInterface $view The view instance used for rendering the file. - * @param string $template The template file. - * @param array $parameters The parameters to be passed to the view file. - * * @return string The rendering result. */ - public function render(ViewInterface $view, string $template, array $parameters): string; + public function render(Template $template): string; } diff --git a/src/ViewInterface.php b/src/ViewInterface.php index 35ac79038..8977118fc 100644 --- a/src/ViewInterface.php +++ b/src/ViewInterface.php @@ -28,7 +28,7 @@ public function withBasePath(string $basePath): static; * corresponding supported file extensions. * * ```php - * $view = $view->withRenderers(['twig' => new \Yiisoft\Yii\Twig\ViewRenderer($environment)]); + * $view = $view->withRenderers(['twig' => new \Yiisoft\View\Twig\TemplateRenderer($environment)]); * ``` * * If no renderer is available for the given view file, the view file will be treated as a normal PHP diff --git a/src/ViewTrait.php b/src/ViewTrait.php index 8bcfeb134..10ac72cfd 100644 --- a/src/ViewTrait.php +++ b/src/ViewTrait.php @@ -397,7 +397,7 @@ public function render(string $view, array $parameters = []): string * Renders a view file. * * If the theme was set {@see setTheme()}, it will try to render the themed version of the view file - * as long as it is available. + * as long as it's available. * * If the renderer was set {@see withRenderers()}, the method will use it to render the view file. Otherwise, * it will simply include the view file as a normal PHP file, capture its output and return it as a string. @@ -407,7 +407,7 @@ public function render(string $view, array $parameters = []): string * file. * * @throws Throwable - * @throws ViewNotFoundException If the view file does not exist + * @throws ViewNotFoundException If the view file doesn't exist * * @return string The rendering result. */ @@ -438,7 +438,9 @@ public function renderFile(string $viewFile, array $parameters = []): string if ($this->beforeRender($viewFile, $parameters)) { $ext = pathinfo($viewFile, PATHINFO_EXTENSION); $renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer(); - $output = $renderer->render($this, $viewFile, $parameters); + $output = $renderer->render( + new Template(path: $viewFile, parameters: $parameters, view: $this, viewContext: $this->context) + ); $output = $this->afterRender($viewFile, $parameters, $output); } diff --git a/tests/PhpTemplateRendererTest.php b/tests/PhpTemplateRendererTest.php index 0f764c229..48ad23224 100644 --- a/tests/PhpTemplateRendererTest.php +++ b/tests/PhpTemplateRendererTest.php @@ -7,6 +7,7 @@ use LogicException; use PHPUnit\Framework\TestCase; use Yiisoft\View\PhpTemplateRenderer; +use Yiisoft\View\Template; use Yiisoft\View\Tests\TestSupport\TestHelper; final class PhpTemplateRendererTest extends TestCase @@ -20,7 +21,7 @@ public function testExceptionDuringRendering(): void $obInitialLevel = ob_get_level(); try { - $renderer->render($view, __DIR__ . '/public/view/error.php', []); + $renderer->render(new Template(path: __DIR__ . '/public/view/error.php', parameters: [], view: $view)); } catch (LogicException) { } diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php new file mode 100644 index 000000000..b6cb29d8f --- /dev/null +++ b/tests/TemplateTest.php @@ -0,0 +1,28 @@ + 'bar'], + $view = TestHelper::createView(), + new ViewContext(__DIR__) + ); + + $this->assertSame($file, $template->getPath()); + $this->assertSame(['foo' => 'bar'], $template->getParameters()); + $this->assertSame($view, $template->getView()); + $this->assertSame(__DIR__, $template->getViewContext()->getViewPath()); + } +}