Final Class Yiisoft\Yii\View\Renderer\ViewRenderer
| Inheritance | Yiisoft\Yii\View\Renderer\ViewRenderer |
|---|---|
| Implements | Yiisoft\View\ViewContextInterface |
ViewRenderer renders the view.
If {@see \Yiisoft\Yii\View\Renderer\ViewRenderer::render()} or {@see \Yiisoft\Yii\View\Renderer\ViewRenderer::renderPartial()} methods are called, an instance of {@see \Yiisoft\DataResponse\DataResponse} is returned. It supports deferred rendering that occurs when calling {@see \Yiisoft\DataResponse\DataResponse::getBody()} or {@see \Yiisoft\DataResponse\DataResponse::getData()}.
If {@see \Yiisoft\Yii\View\Renderer\ViewRenderer::renderAsString()} or {@see \Yiisoft\Yii\View\Renderer\ViewRenderer::renderPartialAsString()} methods are called, the rendering will occur immediately and the string result of the rendering will be returned.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Yii\View\Renderer\ViewRenderer | |
| getViewPath() | Returns a path to a base directory of view templates that is prefixed to the relative view name. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| render() | Returns a response instance {@see DataResponse} that supports deferred rendering. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| renderAsString() | Renders a view as a string. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| renderPartial() | Returns a response instance {@see DataResponse} that supports deferred rendering {@see render()} without applying a layout. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| renderPartialAsString() | Renders a view as string {@see renderAsString()} without applying a layout. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| withAddedInjections() | Return a new instance with the appended specified injections. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| withController() | Extracts the controller name and returns a new instance with the controller name. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| withControllerName() | Returns a new instance with the specified controller name. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| withInjections() | Returns a new instance with the specified injections. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| withLayout() | Returns a new instance with the specified layout. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| withLocale() | Returns a new instance with specified locale code. | Yiisoft\Yii\View\Renderer\ViewRenderer |
| withViewPath() | Returns a new instance with the specified view path. | Yiisoft\Yii\View\Renderer\ViewRenderer |
Method Details
| public mixed __construct ( \Yiisoft\DataResponse\DataResponseFactoryInterface $responseFactory, \Yiisoft\Aliases\Aliases $aliases, \Yiisoft\View\WebView $view, string|null $viewPath = null, string|null $layout = null, array $injections = [], Yiisoft\Yii\View\Renderer\InjectionContainer\InjectionContainerInterface|null $injectionContainer = null ) | ||
| $responseFactory | \Yiisoft\DataResponse\DataResponseFactoryInterface |
The data response factory instance. |
| $aliases | \Yiisoft\Aliases\Aliases |
The aliases instance. |
| $view | \Yiisoft\View\WebView |
The web view instance. |
| $viewPath | string|null |
The full path to the directory of views or its alias. If null, relative view paths in {@see \Yiisoft\Yii\View\Renderer\ViewRenderer::render()} are not available. |
| $layout | string|null |
The full path to the layout file to be applied to views. If null, the layout will not be applied. |
| $injections | array |
The injection instances or class names. |
| $injectionContainer | Yiisoft\Yii\View\Renderer\InjectionContainer\InjectionContainerInterface|null | |
public function __construct(
private DataResponseFactoryInterface $responseFactory,
private Aliases $aliases,
private WebView $view,
?string $viewPath = null,
private ?string $layout = null,
private array $injections = [],
?InjectionContainerInterface $injectionContainer = null,
) {
$this->injectionContainer = $injectionContainer ?? new StubInjectionContainer();
$this->setViewPath($viewPath);
}
Returns a path to a base directory of view templates that is prefixed to the relative view name.
If a controller name has been set {@see withController(), withControllerName()}, it will be appended to the path.
| public string getViewPath ( ) | ||
| return | string |
View templates base directory. |
|---|---|---|
public function getViewPath(): string
{
if ($this->viewPath === null) {
throw new LogicException('The view path is not set.');
}
return $this->aliases->get($this->viewPath) . ($this->name ? '/' . $this->name : '');
}
Returns a response instance {@see DataResponse} that supports deferred rendering.
Rendering will occur when calling {@see \Yiisoft\DataResponse\DataResponse::getBody()} or {@see \Yiisoft\DataResponse\DataResponse::getData()}.
| public \Yiisoft\DataResponse\DataResponse render ( string $view, array $parameters = [] ) | ||
| $view | string |
The view name {@see \Yiisoft\View\WebView::render()}. |
| $parameters | array |
The parameters (name-value pairs) that will be extracted and made available in the view file. |
| return | \Yiisoft\DataResponse\DataResponse |
The response instance. |
|---|---|---|
public function render(string $view, array $parameters = []): DataResponse
{
$commonParameters = $this->getCommonParameters();
$layoutParameters = $this->getLayoutParameters();
$metaTags = $this->getMetaTags();
$linkTags = $this->getLinkTags();
return $this->responseFactory->createResponse(fn (): string => $this->renderProxy(
$view,
$parameters,
$commonParameters,
$layoutParameters,
$metaTags,
$linkTags,
));
}
Renders a view as a string.
| public string renderAsString ( string $view, array $parameters = [] ) | ||
| $view | string |
The view name {@see \Yiisoft\View\WebView::render()}. |
| $parameters | array |
The parameters (name-value pairs) that will be extracted and made available in the view file. |
| return | string |
The rendering result. |
|---|---|---|
| throws | Throwable |
If an error occurred during rendering. |
| throws | \Yiisoft\View\Exception\ViewNotFoundException |
If the view file does not exist. |
| throws | RuntimeException |
If the view cannot be resolved. |
public function renderAsString(string $view, array $parameters = []): string
{
return $this->renderProxy(
$view,
$parameters,
$this->getCommonParameters(),
$this->getLayoutParameters(),
$this->getMetaTags(),
$this->getLinkTags(),
);
}
Returns a response instance {@see DataResponse} that supports deferred rendering {@see render()} without applying a layout.
Rendering will occur when calling {@see \Yiisoft\DataResponse\DataResponse::getBody()} or {@see \Yiisoft\DataResponse\DataResponse::getData()}.
| public \Yiisoft\DataResponse\DataResponse renderPartial ( string $view, array $parameters = [] ) | ||
| $view | string |
The view name {@see \Yiisoft\View\WebView::render()}. |
| $parameters | array |
The parameters (name-value pairs) that will be extracted and made available in the view file. |
| return | \Yiisoft\DataResponse\DataResponse |
The response instance. |
|---|---|---|
public function renderPartial(string $view, array $parameters = []): DataResponse
{
if ($this->layout === null) {
return $this->render($view, $parameters);
}
return $this
->withLayout(null)
->render($view, $parameters);
}
Renders a view as string {@see renderAsString()} without applying a layout.
| public string renderPartialAsString ( string $view, array $parameters = [] ) | ||
| $view | string |
The view name {@see \Yiisoft\View\WebView::render()}. |
| $parameters | array |
The parameters (name-value pairs) that will be extracted and made available in the view file. |
| return | string |
The rendering result. |
|---|---|---|
| throws | Throwable |
If an error occurred during rendering. |
| throws | \Yiisoft\View\Exception\ViewNotFoundException |
If the view file does not exist. |
| throws | RuntimeException |
If the view cannot be resolved. |
public function renderPartialAsString(string $view, array $parameters = []): string
{
if ($this->layout === null) {
return $this->renderAsString($view, $parameters);
}
return $this
->withLayout(null)
->renderAsString($view, $parameters);
}
Return a new instance with the appended specified injections.
| public self withAddedInjections ( object|string $injections ) | ||
| $injections | object|string |
The injection instances or class names. |
public function withAddedInjections(object|string ...$injections): self
{
$new = clone $this;
$new->setInjections(array_merge($this->injections, $injections));
return $new;
}
Extracts the controller name and returns a new instance with the controller name.
| public self withController ( object $controller ) | ||
| $controller | object |
The controller instance. |
public function withController(object $controller): self
{
$new = clone $this;
$new->name = $this->extractControllerName($controller);
return $new;
}
Returns a new instance with the specified controller name.
| public self withControllerName ( string $name ) | ||
| $name | string |
The controller name. |
public function withControllerName(string $name): self
{
$new = clone $this;
$new->name = $name;
return $new;
}
Returns a new instance with the specified injections.
| public self withInjections ( object|string $injections ) | ||
| $injections | object|string |
The injection instances or class names. |
public function withInjections(object|string ...$injections): self
{
$new = clone $this;
$new->setInjections($injections);
return $new;
}
Returns a new instance with the specified layout.
| public self withLayout ( string|null $layout ) | ||
| $layout | string|null |
The full path to the layout file to be applied to views. If null, the layout will not be applied. |
public function withLayout(?string $layout): self
{
$new = clone $this;
$new->layout = $layout;
return $new;
}
Returns a new instance with specified locale code.
| public self withLocale ( string $locale ) | ||
| $locale | string |
The locale code. |
public function withLocale(string $locale): self
{
$new = clone $this;
$new->locale = $locale;
return $new;
}
Returns a new instance with the specified view path.
| public self withViewPath ( string $viewPath ) | ||
| $viewPath | string |
The full path to the directory of views or its alias. |
public function withViewPath(string $viewPath): self
{
$new = clone $this;
$new->setViewPath($viewPath);
return $new;
}
Signup or Login in order to comment.