Final Class Yiisoft\View\WebView
| Inheritance | Yiisoft\View\WebView |
|---|---|
| Implements | Yiisoft\View\ViewInterface |
| Uses Traits | Yiisoft\View\ViewTrait |
WebView represents an instance of a view for use in a WEB environment.
WebView provides a set of methods (e.g. {@see \Yiisoft\View\WebView::render()}) for rendering purpose.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\View\WebView | |
| addCssFiles() | It processes the CSS configuration generated by the asset manager and converts it into HTML code. | Yiisoft\View\WebView |
| addCssStrings() | It processes the CSS strings generated by the asset manager. | Yiisoft\View\WebView |
| addJsFiles() | It processes the JS configuration generated by the asset manager and converts it into HTML code. | Yiisoft\View\WebView |
| addJsStrings() | It processes the JS strings generated by the asset manager. | Yiisoft\View\WebView |
| addJsVars() | It processes the JS variables generated by the asset manager and converts it into JS code. | Yiisoft\View\WebView |
| addToParameter() | Add values to end of common array parameter. If specified parameter does not exist or him is not array, then parameter will be added as empty array. | Yiisoft\View\ViewTrait |
| beginBody() | Marks the beginning of an HTML body section. | Yiisoft\View\WebView |
| beginPage() | Marks the beginning of an HTML page. | Yiisoft\View\WebView |
| clear() | Clears the data for working with the event loop. | Yiisoft\View\ViewTrait |
| deepClone() | Returns a new instance with deep clone of the object, including state cloning. | Yiisoft\View\WebView |
| endBody() | Marks the ending of an HTML body section. | Yiisoft\View\WebView |
| endPage() | Marks the ending of an HTML page. | Yiisoft\View\WebView |
| getBasePath() | Gets the base path to the view directory. | Yiisoft\View\ViewTrait |
| getBlock() | Gets content of the block by ID. | Yiisoft\View\ViewTrait |
| getFallbackExtensions() | Gets the fallback view file extension. | Yiisoft\View\ViewTrait |
| getLocale() | Get the specified locale code. | Yiisoft\View\ViewTrait |
| getParameter() | Gets a common parameter value by ID. | Yiisoft\View\ViewTrait |
| getPlaceholderSignature() | Gets the placeholder signature. | Yiisoft\View\ViewTrait |
| getTheme() | Gets the theme instance, or null if no theme has been set. |
Yiisoft\View\ViewTrait |
| getTitle() | Get title in views. | Yiisoft\View\WebView |
| getViewFile() | Gets the view file currently being rendered. | Yiisoft\View\ViewTrait |
| hasBlock() | Checks the existence of a content block by ID. | Yiisoft\View\ViewTrait |
| hasParameter() | Checks the existence of a common parameter by ID. | Yiisoft\View\ViewTrait |
| head() | Marks the position of an HTML head section. | Yiisoft\View\WebView |
| localize() | Returns the localized version of a specified file. | Yiisoft\View\ViewTrait |
| registerCss() | Registers a CSS code block. | Yiisoft\View\WebView |
| registerCssFile() | Registers a CSS file. | Yiisoft\View\WebView |
| registerCssFromFile() | Registers a CSS code block from file. | Yiisoft\View\WebView |
| registerJs() | Registers a JS code block. | Yiisoft\View\WebView |
| registerJsFile() | Registers a JS file. | Yiisoft\View\WebView |
| registerJsVar() | Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names. | Yiisoft\View\WebView |
| registerLink() | Registers a link tag. | Yiisoft\View\WebView |
| registerLinkTag() | Registers a {@see Link} tag. | Yiisoft\View\WebView |
| registerMeta() | Registers a meta tag. | Yiisoft\View\WebView |
| registerMetaTag() | Registers a {@see Meta} tag. | Yiisoft\View\WebView |
| registerScriptTag() | Register a script tag |
Yiisoft\View\WebView |
| registerStyleTag() | Register a {@see Style} tag. | Yiisoft\View\WebView |
| removeBlock() | Removes a content block. | Yiisoft\View\ViewTrait |
| removeParameter() | Removes a common parameter. | Yiisoft\View\ViewTrait |
| render() | Renders a view. | Yiisoft\View\ViewTrait |
| renderAjax() | Renders a view in response to an AJAX request. | Yiisoft\View\WebView |
| renderAjaxString() | Renders a string in response to an AJAX request. | Yiisoft\View\WebView |
| setBlock() | Sets a content block. | Yiisoft\View\ViewTrait |
| setLocale() | Set the specified locale code. | Yiisoft\View\ViewTrait |
| setParameter() | Sets a common parameter that is accessible in all view templates. | Yiisoft\View\ViewTrait |
| setParameters() | Sets a common parameters that is accessible in all view templates. | Yiisoft\View\ViewTrait |
| setTheme() | Set the specified theme instance. | Yiisoft\View\ViewTrait |
| setTitle() | Set title in views. | Yiisoft\View\WebView |
| withBasePath() | Returns a new instance with specified base path to the view directory. | Yiisoft\View\ViewTrait |
| withClearedState() | Returns a new instance with cleared state (blocks, parameters, registered CSS/JS, etc.) | Yiisoft\View\WebView |
| withContext() | Returns a new instance with the specified view context instance. | Yiisoft\View\ViewTrait |
| withContextPath() | Returns a new instance with the specified view context path. | Yiisoft\View\ViewTrait |
| withFallbackExtension() | Returns a new instance with the specified fallback view file extension. | Yiisoft\View\ViewTrait |
| withLocale() | Set the specified locale code. | Yiisoft\View\ViewTrait |
| withPlaceholderSalt() | Returns a new instance with specified salt for the placeholder signature {@see getPlaceholderSignature()}. | Yiisoft\View\ViewTrait |
| withRenderers() | Returns a new instance with the specified renderers. | Yiisoft\View\ViewTrait |
| withSourceLocale() | Returns a new instance with the specified source locale. | Yiisoft\View\ViewTrait |
| withTheme() | Yiisoft\View\ViewTrait |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| createAfterRenderEvent() | Yiisoft\View\WebView | |
| createBeforeRenderEvent() | Yiisoft\View\WebView |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| PHP_EXTENSION | 'php' | Yiisoft\View\ViewInterface | |
| PLACEHOLDER_BODY_BEGIN | '' | This is internally used as the placeholder for receiving the content registered for the beginning of the body section. | Yiisoft\View\WebView |
| PLACEHOLDER_BODY_END | '' | This is internally used as the placeholder for receiving the content registered for the end of the body section. | Yiisoft\View\WebView |
| PLACEHOLDER_HEAD | '' | This is internally used as the placeholder for receiving the content registered for the head section. | Yiisoft\View\WebView |
| POSITION_BEGIN | 2 | This means the location is at the beginning of the body section. | Yiisoft\View\WebView |
| POSITION_END | 3 | This means the location is at the end of the body section. | Yiisoft\View\WebView |
| POSITION_HEAD | 1 | This means the location is in the head section. | Yiisoft\View\WebView |
| POSITION_LOAD | 5 | This means the JavaScript code block will be executed when HTML page is completely loaded. | Yiisoft\View\WebView |
| POSITION_READY | 4 | This means the JavaScript code block will be executed when HTML document composition is ready. | Yiisoft\View\WebView |
Method Details
| public mixed __construct ( string|null $basePath = null, \Psr\EventDispatcher\EventDispatcherInterface|null $eventDispatcher = null ) | ||
| $basePath | string|null |
The full path to the base directory of views. |
| $eventDispatcher | \Psr\EventDispatcher\EventDispatcherInterface|null |
The event dispatcher instance. |
public function __construct(?string $basePath = null, ?EventDispatcherInterface $eventDispatcher = null)
{
$this->basePath = $basePath;
$this->state = new WebViewState();
$this->localeState = new LocaleState();
$this->themeState = new ThemeState();
$this->eventDispatcher = $eventDispatcher;
$this->setPlaceholderSalt(__DIR__);
}
It processes the CSS configuration generated by the asset manager and converts it into HTML code.
| public self addCssFiles ( array $cssFiles ) | ||
| $cssFiles | array | |
public function addCssFiles(array $cssFiles): self
{
$this->state->addCssFiles($cssFiles);
return $this;
}
It processes the CSS strings generated by the asset manager.
| public self addCssStrings ( array $cssStrings ) | ||
| $cssStrings | array | |
public function addCssStrings(array $cssStrings): self
{
$this->state->addCssStrings($cssStrings);
return $this;
}
It processes the JS configuration generated by the asset manager and converts it into HTML code.
| public self addJsFiles ( array $jsFiles ) | ||
| $jsFiles | array | |
public function addJsFiles(array $jsFiles): self
{
$this->state->addJsFiles($jsFiles);
return $this;
}
It processes the JS strings generated by the asset manager.
| public self addJsStrings ( array $jsStrings ) | ||
| $jsStrings | array | |
| throws | InvalidArgumentException | |
|---|---|---|
public function addJsStrings(array $jsStrings): self
{
$this->state->addJsStrings($jsStrings);
return $this;
}
It processes the JS variables generated by the asset manager and converts it into JS code.
| public self addJsVars ( array $jsVars ) | ||
| $jsVars | array | |
| throws | InvalidArgumentException | |
|---|---|---|
public function addJsVars(array $jsVars): self
{
$this->state->addJsVars($jsVars);
return $this;
}
Defined in: Yiisoft\View\ViewTrait::addToParameter()
Add values to end of common array parameter. If specified parameter does not exist or him is not array, then parameter will be added as empty array.
| public Yiisoft\View\WebView addToParameter ( string $id, mixed $value ) | ||
| $id | string |
The unique identifier of the parameter. |
| $value | mixed |
Value(s) for add to end of array parameter. |
| throws | InvalidArgumentException |
When specified parameter already exists and is not an array. |
|---|---|---|
public function addToParameter(string $id, mixed ...$value): static
{
$this->state->addToParameter($id, ...$value);
return $this;
}
Marks the beginning of an HTML body section.
| public void beginBody ( ) |
public function beginBody(): void
{
echo sprintf(self::PLACEHOLDER_BODY_BEGIN, $this->getPlaceholderSignature());
$this->eventDispatcher?->dispatch(new BodyBegin($this));
}
Marks the beginning of an HTML page.
| public void beginPage ( ) |
public function beginPage(): void
{
ob_start();
ob_implicit_flush(false);
$this->eventDispatcher?->dispatch(new PageBegin($this));
}
Defined in: Yiisoft\View\ViewTrait::clear()
Clears the data for working with the event loop.
| public void clear ( ) |
public function clear(): void
{
$this->viewFiles = [];
$this->state->clear();
$this->localeState = new LocaleState();
$this->themeState = new ThemeState();
}
| protected Yiisoft\View\Event\AfterRenderEventInterface createAfterRenderEvent ( string $viewFile, array $parameters, string $result ) | ||
| $viewFile | string | |
| $parameters | array | |
| $result | string | |
protected function createAfterRenderEvent(
string $viewFile,
array $parameters,
string $result
): AfterRenderEventInterface {
return new AfterRender($this, $viewFile, $parameters, $result);
}
| protected \Psr\EventDispatcher\StoppableEventInterface createBeforeRenderEvent ( string $viewFile, array $parameters ) | ||
| $viewFile | string | |
| $parameters | array | |
protected function createBeforeRenderEvent(string $viewFile, array $parameters): StoppableEventInterface
{
return new BeforeRender($this, $viewFile, $parameters);
}
Returns a new instance with deep clone of the object, including state cloning.
| public Yiisoft\View\WebView deepClone ( ) |
public function deepClone(): static
{
$new = clone $this;
$new->state = clone $this->state;
$new->localeState = clone $this->localeState;
$new->themeState = clone $this->themeState;
return $new;
}
Marks the ending of an HTML body section.
| public void endBody ( ) |
public function endBody(): void
{
$this->eventDispatcher?->dispatch(new BodyEnd($this));
echo sprintf(self::PLACEHOLDER_BODY_END, $this->getPlaceholderSignature());
}
Marks the ending of an HTML page.
| public void endPage ( boolean $ajaxMode = false ) | ||
| $ajaxMode | boolean |
Whether the view is rendering in AJAX mode. If true, the JS scripts registered at {@see \Yiisoft\View\POSITION_READY} and {@see \Yiisoft\View\POSITION_LOAD} positions will be rendered at the end of the view like normal scripts. |
public function endPage(bool $ajaxMode = false): void
{
$this->eventDispatcher?->dispatch(new PageEnd($this));
/**
* @var string $content We assume that in this case active output buffer is always existed, so `ob_get_clean()`
* returns a string.
*/
$content = ob_get_clean();
echo strtr($content, [
sprintf(self::PLACEHOLDER_HEAD, $this->getPlaceholderSignature()) => $this->renderHeadHtml(),
sprintf(self::PLACEHOLDER_BODY_BEGIN, $this->getPlaceholderSignature()) => $this->renderBodyBeginHtml(),
sprintf(self::PLACEHOLDER_BODY_END, $this->getPlaceholderSignature()) => $this->renderBodyEndHtml($ajaxMode),
]);
$this->state->clear();
}
Defined in: Yiisoft\View\ViewTrait::getBasePath()
Gets the base path to the view directory.
| public string getBasePath ( ) | ||
| return | string |
The base view path. |
|---|---|---|
public function getBasePath(): string
{
if ($this->basePath === null) {
throw new LogicException('The base path is not set.');
}
return $this->basePath;
}
Defined in: Yiisoft\View\ViewTrait::getBlock()
Gets content of the block by ID.
| public string getBlock ( string $id ) | ||
| $id | string |
The unique identifier of the block. |
| return | string |
The content of the block. |
|---|---|---|
public function getBlock(string $id): string
{
return $this->state->getBlock($id);
}
Defined in: Yiisoft\View\ViewTrait::getFallbackExtensions()
Gets the fallback view file extension.
| public string[] getFallbackExtensions ( ) | ||
| return | string[] |
The fallback view file extension. |
|---|---|---|
public function getFallbackExtensions(): array
{
return $this->fallbackExtensions;
}
Defined in: Yiisoft\View\ViewTrait::getLocale()
Get the specified locale code.
| public string getLocale ( ) | ||
| return | string |
The locale code. |
|---|---|---|
public function getLocale(): string
{
return $this->localeState->getLocale();
}
Defined in: Yiisoft\View\ViewTrait::getParameter()
Gets a common parameter value by ID.
| public mixed getParameter ( string $id, mixed $default ) | ||
| $id | string |
The unique identifier of the parameter. |
| $default | mixed |
The default value to be returned if the specified parameter does not exist. |
| return | mixed |
The value of the parameter. |
|---|---|---|
| throws | InvalidArgumentException |
If specified parameter does not exist and not passed default value. |
public function getParameter(string $id, mixed ...$default): mixed
{
return call_user_func([$this->state, 'getParameter'], $id, ...$default);
}
Defined in: Yiisoft\View\ViewTrait::getPlaceholderSignature()
Gets the placeholder signature.
| public string getPlaceholderSignature ( ) | ||
| return | string |
The placeholder signature. |
|---|---|---|
public function getPlaceholderSignature(): string
{
return $this->placeholderSignature;
}
Defined in: Yiisoft\View\ViewTrait::getTheme()
Gets the theme instance, or null if no theme has been set.
| public Yiisoft\View\Theme|null getTheme ( ) | ||
| return | Yiisoft\View\Theme|null |
The theme instance, or |
|---|---|---|
public function getTheme(): ?Theme
{
return $this->themeState->getTheme();
}
Get title in views.
in Layout:
<title><?= Html::encode($this->getTitle()) ?></title>
in Views:
$this->setTitle('Web Application - Yii 3.0.');
| public string getTitle ( ) |
public function getTitle(): string
{
return $this->state->getTitle();
}
Defined in: Yiisoft\View\ViewTrait::getViewFile()
Gets the view file currently being rendered.
| public string|null getViewFile ( ) | ||
| return | string|null |
The view file currently being rendered. |
|---|---|---|
public function getViewFile(): ?string
{
/** @psalm-suppress InvalidArrayOffset */
return empty($this->viewFiles) ? null : end($this->viewFiles)['resolved'];
}
Defined in: Yiisoft\View\ViewTrait::hasBlock()
Checks the existence of a content block by ID.
| public boolean hasBlock ( string $id ) | ||
| $id | string |
The unique identifier of the block. |
| return | boolean |
Whether a content block exists. |
|---|---|---|
public function hasBlock(string $id): bool
{
return $this->state->hasBlock($id);
}
Defined in: Yiisoft\View\ViewTrait::hasParameter()
Checks the existence of a common parameter by ID.
| public boolean hasParameter ( string $id ) | ||
| $id | string |
The unique identifier of the parameter. |
| return | boolean |
Whether a custom parameter that is common for all view templates exists. |
|---|---|---|
public function hasParameter(string $id): bool
{
return $this->state->hasParameter($id);
}
Marks the position of an HTML head section.
| public void head ( ) |
public function head(): void
{
echo sprintf(self::PLACEHOLDER_HEAD, $this->getPlaceholderSignature());
$this->eventDispatcher?->dispatch(new Head($this));
}
Defined in: Yiisoft\View\ViewTrait::localize()
Returns the localized version of a specified file.
The searching is based on the specified locale code. In particular, a file with the same name will be looked for under the subdirectory whose name is the same as the locale code. For example, given the file "path/to/view.php" and locale code "zh-CN", the localized file will be looked for as "path/to/zh-CN/view.php". If the file is not found, it will try a fallback with just a locale code that is "zh" i.e. "path/to/zh/view.php". If it is not found as well the original file will be returned.
If the target and the source locale codes are the same, the original file will be returned.
| public string localize ( string $file, string|null $locale = null, string|null $sourceLocale = null ) | ||
| $file | string |
The original file |
| $locale | string|null |
The target locale that the file should be localized to. |
| $sourceLocale | string|null |
The locale that the original file is in. |
| return | string |
The matching localized file, or the original file if the localized version is not found. If the target and the source locale codes are the same, the original file will be returned. |
|---|---|---|
public function localize(string $file, ?string $locale = null, ?string $sourceLocale = null): string
{
$locale ??= $this->localeState->getLocale();
$sourceLocale ??= $this->sourceLocale;
if ($locale === $sourceLocale) {
return $file;
}
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . basename($file);
if (is_file($desiredFile)) {
return $desiredFile;
}
$locale = substr($locale, 0, 2);
if ($locale === $sourceLocale) {
return $file;
}
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . basename($file);
return is_file($desiredFile) ? $desiredFile : $file;
}
Registers a CSS code block.
| public self registerCss ( string $css, integer $position = self::POSITION_HEAD, array $attributes = [], string|null $key = null ) | ||
| $css | string |
The content of the CSS code block to be registered. |
| $position | integer | |
| $attributes | array |
The HTML attributes for the {@see \Yiisoft\Html\Tag\Style} tag. |
| $key | string|null |
The key that identifies the CSS code block. If null, it will use $css as the key. If two CSS code blocks are registered with the same key, the latter will overwrite the former. |
public function registerCss(
string $css,
int $position = self::POSITION_HEAD,
array $attributes = [],
?string $key = null
): self {
$this->state->registerCss($css, $position, $attributes, $key);
return $this;
}
Registers a CSS file.
This method should be used for simple registration of CSS files. If you want to use features of {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use {@see \Yiisoft\Assets\AssetBundle}.
| public self registerCssFile ( string $url, integer $position = self::POSITION_HEAD, array $options = [], string|null $key = null ) | ||
| $url | string |
The CSS file to be registered. |
| $position | integer | |
| $options | array |
The HTML attributes for the link tag. Please refer to {@see \Yiisoft\Html\Html::cssFile()} for the supported options. |
| $key | string|null |
The key that identifies the CSS script file. If null, it will use $url as the key. If two CSS files are registered with the same key, the latter will overwrite the former. |
public function registerCssFile(
string $url,
int $position = self::POSITION_HEAD,
array $options = [],
?string $key = null
): self {
$this->state->registerCssFile($url, $position, $options, $key);
return $this;
}
Registers a CSS code block from file.
See also registerCss().
| public self registerCssFromFile ( string $path, integer $position = self::POSITION_HEAD, array $attributes = [], string|null $key = null ) | ||
| $path | string |
The path or URL to CSS file. |
| $position | integer | |
| $attributes | array | |
| $key | string|null | |
public function registerCssFromFile(
string $path,
int $position = self::POSITION_HEAD,
array $attributes = [],
?string $key = null
): self {
$this->state->registerCssFromFile($path, $position, $attributes, $key);
return $this;
}
Registers a JS code block.
| public self registerJs ( string $js, integer $position = self::POSITION_END, string|null $key = null ) | ||
| $js | string |
The JS code block to be registered |
| $position | integer |
The position at which the JS script tag should be inserted in a page. The possible values are:
|
| $key | string|null |
The key that identifies the JS code block. If null, it will use $js as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former. |
public function registerJs(string $js, int $position = self::POSITION_END, ?string $key = null): self
{
$this->state->registerJs($js, $position, $key);
return $this;
}
Registers a JS file.
This method should be used for simple registration of JS files. If you want to use features of {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use {@see \Yiisoft\Assets\AssetBundle}.
| public self registerJsFile ( string $url, integer $position = self::POSITION_END, array $options = [], string|null $key = null ) | ||
| $url | string |
The JS file to be registered. |
| $position | integer | |
| $options | array |
The HTML attributes for the script tag. The following options are specially handled and are not treated as HTML attributes:
Please refer to {@see \Yiisoft\Html\Html::javaScriptFile()} for other supported options. |
| $key | string|null |
The key that identifies the JS script file. If null, it will use $url as the key. If two JS files are registered with the same key at the same position, the latter will overwrite the former. Note that position option takes precedence, thus files registered with the same key, but different position option will not override each other. |
public function registerJsFile(
string $url,
int $position = self::POSITION_END,
array $options = [],
?string $key = null
): self {
$this->state->registerJsFile($url, $position, $options, $key);
return $this;
}
Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names.
| public self registerJsVar ( string $name, mixed $value, integer $position = self::POSITION_HEAD ) | ||
| $name | string |
Name of the variable |
| $value | mixed |
Value of the variable |
| $position | integer |
The position in a page at which the JavaScript variable should be inserted. The possible values are:
|
public function registerJsVar(string $name, mixed $value, int $position = self::POSITION_HEAD): self
{
$this->state->registerJsVar($name, $value, $position);
return $this;
}
Registers a link tag.
For example, a link tag for a custom favicon can be added like the following:
$view->registerLink(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']);
which will result in the following HTML: <link rel="icon" type="image/png" href="/myicon.png">.
Note: To register link tags for CSS stylesheets, use {@see registerCssFile()]} instead, which has more options for this kind of link tag.
| public self registerLink ( array $attributes, integer $position = self::POSITION_HEAD, string|null $key = null ) | ||
| $attributes | array |
The HTML attributes for the link tag. |
| $position | integer |
The position at which the link tag should be inserted in a page. |
| $key | string|null |
The key that identifies the link tag. If two link tags are registered with the same key, the latter will overwrite the former. If this is null, the new link tag will be appended to the existing ones. |
public function registerLink(array $attributes, int $position = self::POSITION_HEAD, ?string $key = null): self
{
$this->state->registerLink($attributes, $position, $key);
return $this;
}
Registers a {@see Link} tag.
See also registerLink().
| public self registerLinkTag ( \Yiisoft\Html\Tag\Link $link, integer $position = self::POSITION_HEAD, string|null $key = null ) | ||
| $link | \Yiisoft\Html\Tag\Link | |
| $position | integer | |
| $key | string|null | |
public function registerLinkTag(Link $link, int $position = self::POSITION_HEAD, ?string $key = null): self
{
$this->state->registerLinkTag($link, $position, $key);
return $this;
}
Registers a meta tag.
For example, a description meta tag can be added like the following:
$view->registerMeta([
'name' => 'description',
'content' => 'This website is about funny raccoons.'
]);
will result in the meta tag <meta name="description" content="This website is about funny raccoons.">.
| public self registerMeta ( array $attributes, string|null $key = null ) | ||
| $attributes | array |
The HTML attributes for the meta tag. |
| $key | string|null |
The key that identifies the meta tag. If two meta tags are registered with the same key, the latter will overwrite the former. If this is null, the new meta tag will be appended to the existing ones. |
public function registerMeta(array $attributes, ?string $key = null): self
{
$this->state->registerMeta($attributes, $key);
return $this;
}
Registers a {@see Meta} tag.
See also registerMeta().
| public self registerMetaTag ( \Yiisoft\Html\Tag\Meta $meta, string|null $key = null ) | ||
| $meta | \Yiisoft\Html\Tag\Meta | |
| $key | string|null | |
public function registerMetaTag(Meta $meta, ?string $key = null): self
{
$this->state->registerMetaTag($meta, $key);
return $this;
}
Register a script tag
See also registerJs().
| public self registerScriptTag ( \Yiisoft\Html\Tag\Script $script, integer $position = self::POSITION_END, string|null $key = null ) | ||
| $script | \Yiisoft\Html\Tag\Script | |
| $position | integer | |
| $key | string|null | |
public function registerScriptTag(Script $script, int $position = self::POSITION_END, ?string $key = null): self
{
$this->state->registerScriptTag($script, $position, $key);
return $this;
}
Register a {@see Style} tag.
See also registerJs().
| public self registerStyleTag ( \Yiisoft\Html\Tag\Style $style, integer $position = self::POSITION_HEAD, string|null $key = null ) | ||
| $style | \Yiisoft\Html\Tag\Style | |
| $position | integer | |
| $key | string|null | |
public function registerStyleTag(Style $style, int $position = self::POSITION_HEAD, ?string $key = null): self
{
$this->state->registerStyleTag($style, $position, $key);
return $this;
}
Defined in: Yiisoft\View\ViewTrait::removeBlock()
Removes a content block.
| public Yiisoft\View\WebView removeBlock ( string $id ) | ||
| $id | string |
The unique identifier of the block. |
public function removeBlock(string $id): static
{
$this->state->removeBlock($id);
return $this;
}
Defined in: Yiisoft\View\ViewTrait::removeParameter()
Removes a common parameter.
| public Yiisoft\View\WebView removeParameter ( string $id ) | ||
| $id | string |
The unique identifier of the parameter. |
public function removeParameter(string $id): static
{
$this->state->removeParameter($id);
return $this;
}
Defined in: Yiisoft\View\ViewTrait::render()
Renders a view.
The view to be rendered can be specified in one of the following formats:
- the absolute path to the view file, e.g. "/path/to/view.php";
- the name of the view starting with
//to join the base path {@see \Yiisoft\View\getBasePath()}, e.g. "//site/index"; - the name of the view starting with
./to join the directory containing the view currently being rendered (i.e., this happens when rendering a view within another view), e.g. "./widget"; - the name of the view starting with
../to join the parent directory containing the view currently being rendered, e.g. "../_header"; - the name of the view without the starting
//,./or../(e.g. "site/index"). The corresponding view file will be looked for under the {@see \Yiisoft\View\ViewContextInterface::getViewPath()} of the context set via {@see \Yiisoft\View\withContext()}. If the context instance was not set {@see \Yiisoft\View\withContext()}, it will be looked for under the base path.
Warning: Using .. in view path can lead to accessing unsafe data, e.g., //../../etc/passwd. Ensure that such
cases are handled properly.
| public string render ( string $view, array $parameters = [] ) | ||
| $view | string |
The view name. |
| $parameters | array |
The parameters (name-value pairs) that will be extracted and made available in the view file. |
| return | string |
The rendering result. |
|---|---|---|
| throws | LogicException |
If the view cannot be resolved. |
| throws | Yiisoft\View\Exception\ViewNotFoundException |
If the view file does not exist. |
| throws | Throwable | |
public function render(string $view, array $parameters = []): string
{
$viewFile = $this->findTemplateFile($view);
$parameters = array_merge($this->state->getParameters(), $parameters);
// TODO: these two match now
$requestedFile = $viewFile;
$theme = $this->getTheme();
if ($theme !== null) {
$viewFile = $theme->applyTo($viewFile);
}
if (is_file($viewFile)) {
$viewFile = $this->localize($viewFile);
} else {
throw new ViewNotFoundException("The view file \"$viewFile\" does not exist.");
}
$output = '';
$this->viewFiles[] = [
'resolved' => $viewFile,
'requested' => $requestedFile,
];
if ($this->beforeRender($viewFile, $parameters)) {
$ext = pathinfo($viewFile, PATHINFO_EXTENSION);
$renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer();
$output = $renderer->render($this, $viewFile, $parameters);
$output = $this->afterRender($viewFile, $parameters, $output);
}
array_pop($this->viewFiles);
return $output;
}
Renders a view in response to an AJAX request.
This method is similar to {@see \Yiisoft\View\render()} except that it will surround the view being rendered with the calls of {@see \Yiisoft\View\beginPage()}, {@see \Yiisoft\View\head()}, {@see \Yiisoft\View\beginBody()}, {@see \Yiisoft\View\endBody()} and {@see \Yiisoft\View\endPage()}. By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files that are registered with the view.
| public string renderAjax ( string $view, array $parameters = [] ) | ||
| $view | string |
The view name. Please refer to {@see \Yiisoft\View\render()} on how to specify this parameter. |
| $parameters | array |
The parameters (name-value pairs) that will be extracted and made available in the view file. |
| return | string |
The rendering result {@see \Yiisoft\View\render()} |
|---|---|---|
public function renderAjax(string $view, array $parameters = []): string
{
ob_start();
ob_implicit_flush(false);
$this->beginPage();
$this->head();
$this->beginBody();
echo $this->render($view, $parameters);
$this->endBody();
$this->endPage(true);
/**
* @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();
}
Renders a string in response to an AJAX request.
| public string renderAjaxString ( string $string ) | ||
| $string | string |
The string. |
| return | string |
The rendering result. |
|---|---|---|
public function renderAjaxString(string $string): string
{
ob_start();
ob_implicit_flush(false);
$this->beginPage();
$this->head();
$this->beginBody();
echo $string;
$this->endBody();
$this->endPage(true);
/**
* @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();
}
Defined in: Yiisoft\View\ViewTrait::setBlock()
Sets a content block.
| public Yiisoft\View\WebView setBlock ( string $id, string $content ) | ||
| $id | string |
The unique identifier of the block. |
| $content | string |
The content of the block. |
public function setBlock(string $id, string $content): static
{
$this->state->setBlock($id, $content);
return $this;
}
Defined in: Yiisoft\View\ViewTrait::setLocale()
Set the specified locale code.
| public Yiisoft\View\WebView setLocale ( string $locale ) | ||
| $locale | string |
The locale code. |
public function setLocale(string $locale): static
{
$this->localeState->setLocale($locale);
return $this;
}
Defined in: Yiisoft\View\ViewTrait::setParameter()
Sets a common parameter that is accessible in all view templates.
| public Yiisoft\View\WebView setParameter ( string $id, mixed $value ) | ||
| $id | string |
The unique identifier of the parameter. |
| $value | mixed |
The value of the parameter. |
public function setParameter(string $id, mixed $value): static
{
$this->state->setParameter($id, $value);
return $this;
}
Defined in: Yiisoft\View\ViewTrait::setParameters()
Sets a common parameters that is accessible in all view templates.
See also setParameter().
| public Yiisoft\View\WebView setParameters ( array $parameters ) | ||
| $parameters | array |
Parameters that are common for all view templates. |
public function setParameters(array $parameters): static
{
$this->state->setParameters($parameters);
return $this;
}
Defined in: Yiisoft\View\ViewTrait::setTheme()
Set the specified theme instance.
| public Yiisoft\View\WebView setTheme ( Yiisoft\View\Theme|null $theme ) | ||
| $theme | Yiisoft\View\Theme|null |
The theme instance or |
public function setTheme(?Theme $theme): static
{
$this->themeState->setTheme($theme);
return $this;
}
Set title in views.
{@see \Yiisoft\View\getTitle()}
| public self setTitle ( string|\Stringable $value ) | ||
| $value | string|\Stringable | |
public function setTitle(string|Stringable $value): self
{
$this->state->setTitle((string) $value);
return $this;
}
Defined in: Yiisoft\View\ViewTrait::withBasePath()
Returns a new instance with specified base path to the view directory.
| public Yiisoft\View\WebView withBasePath ( string|null $basePath ) | ||
| $basePath | string|null |
The base path to the view directory. |
public function withBasePath(string|null $basePath): static
{
$new = clone $this;
$new->basePath = $basePath;
return $new;
}
Returns a new instance with cleared state (blocks, parameters, registered CSS/JS, etc.)
| public Yiisoft\View\WebView withClearedState ( ) |
public function withClearedState(): static
{
$new = clone $this;
$new->state = new WebViewState();
$new->localeState = new LocaleState();
$new->themeState = new ThemeState();
return $new;
}
Defined in: Yiisoft\View\ViewTrait::withContext()
Returns a new instance with the specified view context instance.
| public Yiisoft\View\WebView withContext ( Yiisoft\View\ViewContextInterface|null $context ) | ||
| $context | Yiisoft\View\ViewContextInterface|null |
The context under which the {@see \Yiisoft\View\render()} method is being invoked. |
public function withContext(ViewContextInterface|null $context): static
{
$new = clone $this;
$new->context = $context;
$new->viewFiles = [];
return $new;
}
Defined in: Yiisoft\View\ViewTrait::withContextPath()
Returns a new instance with the specified view context path.
| public Yiisoft\View\WebView withContextPath ( string $path ) | ||
| $path | string |
The context path under which the {@see \Yiisoft\View\render()} method is being invoked. |
public function withContextPath(string $path): static
{
return $this->withContext(new ViewContext($path));
}
Defined in: Yiisoft\View\ViewTrait::withFallbackExtension()
Returns a new instance with the specified fallback view file extension.
| public Yiisoft\View\WebView withFallbackExtension ( string $fallbackExtension, string $otherFallbacks ) | ||
| $fallbackExtension | string |
The fallback view file extension. Default is {@see \Yiisoft\View\ViewInterface::PHP_EXTENSION}. This will be appended to view file names if they don't exist. |
| $otherFallbacks | string | |
public function withFallbackExtension(string $fallbackExtension, string ...$otherFallbacks): static
{
$new = clone $this;
$new->fallbackExtensions = [$fallbackExtension, ...array_values($otherFallbacks)];
return $new;
}
Defined in: Yiisoft\View\ViewTrait::withLocale()
Set the specified locale code.
| public Yiisoft\View\WebView withLocale ( string $locale ) | ||
| $locale | string |
The locale code. |
public function withLocale(string $locale): static
{
$new = clone $this;
$new->localeState = new LocaleState($locale);
return $new;
}
Defined in: Yiisoft\View\ViewTrait::withPlaceholderSalt()
Returns a new instance with specified salt for the placeholder signature {@see getPlaceholderSignature()}.
| public Yiisoft\View\WebView withPlaceholderSalt ( string $salt ) | ||
| $salt | string |
The placeholder salt. |
public function withPlaceholderSalt(string $salt): static
{
$new = clone $this;
$new->setPlaceholderSalt($salt);
return $new;
}
Defined in: Yiisoft\View\ViewTrait::withRenderers()
Returns a new instance with the specified renderers.
| public Yiisoft\View\WebView withRenderers ( array $renderers ) | ||
| $renderers | array |
A list of available renderers indexed by their corresponding supported file extensions.
If no renderer is available for the given view file, the view file will be treated as a normal PHP and rendered via {@see \Yiisoft\View\PhpTemplateRenderer}. |
public function withRenderers(array $renderers): static
{
$new = clone $this;
$new->renderers = $renderers;
return $new;
}
Defined in: Yiisoft\View\ViewTrait::withSourceLocale()
Returns a new instance with the specified source locale.
| public Yiisoft\View\WebView withSourceLocale ( string $locale ) | ||
| $locale | string |
The source locale. |
public function withSourceLocale(string $locale): static
{
$new = clone $this;
$new->sourceLocale = $locale;
return $new;
}
Defined in: Yiisoft\View\ViewTrait::withTheme()
| public Yiisoft\View\WebView withTheme ( Yiisoft\View\Theme|null $theme ) | ||
| $theme | Yiisoft\View\Theme|null | |
public function withTheme(?Theme $theme): static
{
$new = clone $this;
$new->themeState = new ThemeState($theme);
return $new;
}
Signup or Login in order to comment.