Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/PhpTemplateRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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) {
Expand Down
65 changes: 65 additions & 0 deletions src/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Yiisoft\View;

/**
* The template holds the information needed to render a view.
*/
final class Template
{
/**
* @param string $path The full absolute path of the view template file.
* @param array $parameters The parameters to pass to the template.
* @param ViewInterface $view The view instance used for rendering the file.
* @param ViewContextInterface|null $viewContext The context instance of the view.
*/
public function __construct(
private string $path,
private array $parameters,
private ViewInterface $view,
private ?ViewContextInterface $viewContext = null
) {
}

/**
* Get the full absolute path of the view template file.
*
* @return string The full absolute path of the view template file.
*/
public function getPath(): string
{
return $this->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;
}
}
6 changes: 1 addition & 5 deletions src/TemplateRendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/ViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/PhpTemplateRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
}

Expand Down
28 changes: 28 additions & 0 deletions tests/TemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Yiisoft\View\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\View\Template;
use Yiisoft\View\Tests\TestSupport\TestHelper;
use Yiisoft\View\ViewContext;

final class TemplateTest extends TestCase
{
public function testTemplate(): void
{
$template = new Template(
$file = __DIR__ . '/public/view/error.php',
['foo' => '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());
}
}