Final Class Yiisoft\View\State\WebViewState
| Inheritance | Yiisoft\View\State\WebViewState |
|---|---|
| Uses Traits | Yiisoft\View\State\StateTrait |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| addCssFiles() | It processes the CSS configuration generated by the asset manager and converts it into HTML code. | Yiisoft\View\State\WebViewState |
| addCssStrings() | It processes the CSS strings generated by the asset manager. | Yiisoft\View\State\WebViewState |
| addJsFiles() | It processes the JS configuration generated by the asset manager and converts it into HTML code. | Yiisoft\View\State\WebViewState |
| addJsStrings() | It processes the JS strings generated by the asset manager. | Yiisoft\View\State\WebViewState |
| addJsVars() | It processes the JS variables generated by the asset manager and converts it into JS code. | Yiisoft\View\State\WebViewState |
| 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\State\StateTrait |
| clear() | Clears the data for working with the event loop: - the added parameters and blocks; - the registered meta tags, link tags, css/js scripts, files and title. | Yiisoft\View\State\WebViewState |
| getBlock() | Gets content of the block by ID. | Yiisoft\View\State\StateTrait |
| getCss() | Yiisoft\View\State\WebViewState | |
| getCssFiles() | Yiisoft\View\State\WebViewState | |
| getJs() | Yiisoft\View\State\WebViewState | |
| getJsFiles() | Yiisoft\View\State\WebViewState | |
| getLinkTags() | Yiisoft\View\State\WebViewState | |
| getMetaTags() | Yiisoft\View\State\WebViewState | |
| getParameter() | Gets a common parameter value by ID. | Yiisoft\View\State\StateTrait |
| getParameters() | Yiisoft\View\State\StateTrait | |
| getTitle() | Get title in views. | Yiisoft\View\State\WebViewState |
| hasBlock() | Checks the existence of a content block by ID. | Yiisoft\View\State\StateTrait |
| hasParameter() | Checks the existence of a common parameter by ID. | Yiisoft\View\State\StateTrait |
| registerCss() | Registers a CSS code block. | Yiisoft\View\State\WebViewState |
| registerCssFile() | Registers a CSS file. | Yiisoft\View\State\WebViewState |
| registerCssFromFile() | Registers a CSS code block from file. | Yiisoft\View\State\WebViewState |
| registerJs() | Registers a JS code block. | Yiisoft\View\State\WebViewState |
| registerJsFile() | Registers a JS file. | Yiisoft\View\State\WebViewState |
| registerJsVar() | Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names. | Yiisoft\View\State\WebViewState |
| registerLink() | Registers a link tag. | Yiisoft\View\State\WebViewState |
| registerLinkTag() | Registers a {@see Link} tag. | Yiisoft\View\State\WebViewState |
| registerMeta() | Registers a meta tag. | Yiisoft\View\State\WebViewState |
| registerMetaTag() | Registers a {@see Meta} tag. | Yiisoft\View\State\WebViewState |
| registerScriptTag() | Register a script tag |
Yiisoft\View\State\WebViewState |
| registerStyleTag() | Register a {@see Style} tag. | Yiisoft\View\State\WebViewState |
| removeBlock() | Removes a content block. | Yiisoft\View\State\StateTrait |
| removeParameter() | Removes a common parameter. | Yiisoft\View\State\StateTrait |
| setBlock() | Sets a content block. | Yiisoft\View\State\StateTrait |
| setParameter() | Sets a common parameter that is accessible in all view templates. | Yiisoft\View\State\StateTrait |
| setParameters() | Sets a common parameters that is accessible in all view templates. | Yiisoft\View\State\StateTrait |
| setTitle() | Set title in views. | Yiisoft\View\State\WebViewState |
Method Details
It processes the CSS configuration generated by the asset manager and converts it into HTML code.
| public void addCssFiles ( array $cssFiles ) | ||
| $cssFiles | array | |
public function addCssFiles(array $cssFiles): void
{
/** @var mixed $value */
foreach ($cssFiles as $key => $value) {
$this->registerCssFileByConfig(
is_string($key) ? $key : null,
is_array($value) ? $value : [$value],
);
}
}
It processes the CSS strings generated by the asset manager.
| public void addCssStrings ( array $cssStrings ) | ||
| $cssStrings | array | |
public function addCssStrings(array $cssStrings): void
{
/** @var mixed $value */
foreach ($cssStrings as $key => $value) {
$this->registerCssStringByConfig(
is_string($key) ? $key : null,
is_array($value) ? $value : [$value, WebView::POSITION_HEAD],
);
}
}
It processes the JS configuration generated by the asset manager and converts it into HTML code.
| public void addJsFiles ( array $jsFiles ) | ||
| $jsFiles | array | |
public function addJsFiles(array $jsFiles): void
{
/** @var mixed $value */
foreach ($jsFiles as $key => $value) {
$this->registerJsFileByConfig(
is_string($key) ? $key : null,
is_array($value) ? $value : [$value],
);
}
}
It processes the JS strings generated by the asset manager.
| public void addJsStrings ( array $jsStrings ) | ||
| $jsStrings | array | |
| throws | InvalidArgumentException | |
|---|---|---|
public function addJsStrings(array $jsStrings): void
{
/** @var mixed $value */
foreach ($jsStrings as $key => $value) {
$this->registerJsStringByConfig(
is_string($key) ? $key : null,
is_array($value) ? $value : [$value, WebView::POSITION_END]
);
}
}
It processes the JS variables generated by the asset manager and converts it into JS code.
| public void addJsVars ( array $jsVars ) | ||
| $jsVars | array | |
| throws | InvalidArgumentException | |
|---|---|---|
public function addJsVars(array $jsVars): void
{
/** @var mixed $value */
foreach ($jsVars as $key => $value) {
if (is_string($key)) {
$this->registerJsVar($key, $value, WebView::POSITION_HEAD);
} else {
$this->registerJsVarByConfig((array) $value);
}
}
}
Defined in: Yiisoft\View\State\StateTrait::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\State\WebViewState 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
{
/** @var mixed $array */
$array = $this->parameters[$id] ?? [];
if (!is_array($array)) {
throw new InvalidArgumentException(
sprintf('The "%s" parameter already exists and is not an array.', $id)
);
}
$this->setParameter($id, array_merge($array, $value));
return $this;
}
Clears the data for working with the event loop: - the added parameters and blocks; - the registered meta tags, link tags, css/js scripts, files and title.
| public void clear ( ) |
public function clear(): void
{
$this->parameters = [];
$this->blocks = [];
$this->title = '';
$this->metaTags = [];
$this->linkTags = [];
$this->css = [];
$this->cssFiles = [];
$this->js = [];
$this->jsFiles = [];
}
Defined in: Yiisoft\View\State\StateTrait::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
{
if (isset($this->blocks[$id])) {
return $this->blocks[$id];
}
throw new InvalidArgumentException('Block "' . $id . '" not found.');
}
| public array getCss ( ) | ||
| return | array |
The registered CSS code blocks. |
|---|---|---|
public function getCss(): array
{
return $this->css;
}
| public array getCssFiles ( ) | ||
| return | array |
The registered CSS files. |
|---|---|---|
public function getCssFiles(): array
{
return $this->cssFiles;
}
| public array getJs ( ) | ||
| return | array |
The registered JS code blocks |
|---|---|---|
public function getJs(): array
{
return $this->js;
}
| public array getJsFiles ( ) | ||
| return | array |
The registered JS files. |
|---|---|---|
public function getJsFiles(): array
{
return $this->jsFiles;
}
| public array getLinkTags ( ) | ||
| return | array |
The registered link tags. |
|---|---|---|
public function getLinkTags(): array
{
return $this->linkTags;
}
| public \Yiisoft\Html\Tag\Meta[] getMetaTags ( ) | ||
| return | \Yiisoft\Html\Tag\Meta[] |
The registered meta tags. |
|---|---|---|
public function getMetaTags(): array
{
return $this->metaTags;
}
Defined in: Yiisoft\View\State\StateTrait::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
{
if (isset($this->parameters[$id])) {
return $this->parameters[$id];
}
if (!empty($default)) {
return reset($default);
}
throw new InvalidArgumentException('Parameter "' . $id . '" not found.');
}
Defined in: Yiisoft\View\State\StateTrait::getParameters()
| public array getParameters ( ) |
public function getParameters(): array
{
return $this->parameters;
}
Get title in views.
| public string getTitle ( ) |
public function getTitle(): string
{
return $this->title;
}
Defined in: Yiisoft\View\State\StateTrait::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 isset($this->blocks[$id]);
}
Defined in: Yiisoft\View\State\StateTrait::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 isset($this->parameters[$id]);
}
Registers a CSS code block.
| public void registerCss ( string $css, integer $position = WebView::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 |
public function registerCss(
string $css,
int $position = WebView::POSITION_HEAD,
array $attributes = [],
?string $key = null
): void {
$this->css[$position][$key ?? md5($css)] = $attributes === [] ? $css : Html::style($css, $attributes);
}
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 void registerCssFile ( string $url, integer $position = WebView::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 |
public function registerCssFile(
string $url,
int $position = WebView::POSITION_HEAD,
array $options = [],
?string $key = null
): void {
if (!$this->isValidCssPosition($position)) {
throw new InvalidArgumentException('Invalid position of CSS file.');
}
$this->cssFiles[$position][$key ?? $url] = Html::cssFile($url, $options)->render();
}
Registers a CSS code block from file.
See also registerCss().
| public void registerCssFromFile ( string $path, integer $position = WebView::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 = WebView::POSITION_HEAD,
array $attributes = [],
?string $key = null
): void {
$css = file_get_contents($path);
if ($css === false) {
throw new RuntimeException(sprintf('File %s could not be read.', $path));
}
$this->registerCss($css, $position, $attributes, $key);
}
Registers a JS code block.
| public void registerJs ( string $js, integer $position = WebView::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 |
public function registerJs(string $js, int $position = WebView::POSITION_END, ?string $key = null): void
{
$this->js[$position][$key ?? md5($js)] = $js;
}
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 void registerJsFile ( string $url, integer $position = WebView::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 = WebView::POSITION_END,
array $options = [],
?string $key = null
): void {
if (!$this->isValidJsPosition($position)) {
throw new InvalidArgumentException('Invalid position of JS file.');
}
$this->jsFiles[$position][$key ?? $url] = Html::javaScriptFile($url, $options)->render();
}
Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names.
| public void registerJsVar ( string $name, mixed $value, integer $position = WebView::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 = WebView::POSITION_HEAD): void
{
$js = sprintf('var %s = %s;', $name, Json::htmlEncode($value));
$this->registerJs($js, $position, $name);
}
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']);
Note: To register link tags for CSS stylesheets, use {@see registerCssFile()]} instead, which has more options for this kind of link tag.
| public void registerLink ( array $attributes, integer $position = WebView::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 = WebView::POSITION_HEAD, ?string $key = null): void
{
$this->registerLinkTag(Html::link()->attributes($attributes), $position, $key);
}
Registers a {@see Link} tag.
See also registerLink().
| public void registerLinkTag ( \Yiisoft\Html\Tag\Link $link, integer $position = WebView::POSITION_HEAD, string|null $key = null ) | ||
| $link | \Yiisoft\Html\Tag\Link | |
| $position | integer | |
| $key | string|null | |
public function registerLinkTag(Link $link, int $position = WebView::POSITION_HEAD, ?string $key = null): void
{
$key === null
? $this->linkTags[$position][] = $link
: $this->linkTags[$position][$key] = $link;
}
Registers a meta tag.
For example, a description meta tag can be added like the following:
$state->registerMeta([
'name' => 'description',
'content' => 'This website is about funny raccoons.'
]);
| public void 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): void
{
$this->registerMetaTag(Html::meta($attributes), $key);
}
Registers a {@see Meta} tag.
See also registerMeta().
| public void 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): void
{
$key === null
? $this->metaTags[] = $meta
: $this->metaTags[$key] = $meta;
}
Register a script tag
See also registerJs().
| public void registerScriptTag ( \Yiisoft\Html\Tag\Script $script, integer $position = WebView::POSITION_END, string|null $key = null ) | ||
| $script | \Yiisoft\Html\Tag\Script | |
| $position | integer | |
| $key | string|null | |
public function registerScriptTag(Script $script, int $position = WebView::POSITION_END, ?string $key = null): void
{
$this->js[$position][$key ?? md5($script->render())] = $script;
}
Register a {@see Style} tag.
See also registerJs().
| public void registerStyleTag ( \Yiisoft\Html\Tag\Style $style, integer $position = WebView::POSITION_HEAD, string|null $key = null ) | ||
| $style | \Yiisoft\Html\Tag\Style | |
| $position | integer | |
| $key | string|null | |
public function registerStyleTag(Style $style, int $position = WebView::POSITION_HEAD, ?string $key = null): void
{
$this->css[$position][$key ?? md5($style->render())] = $style;
}
Defined in: Yiisoft\View\State\StateTrait::removeBlock()
Removes a content block.
| public Yiisoft\View\State\WebViewState removeBlock ( string $id ) | ||
| $id | string |
The unique identifier of the block. |
public function removeBlock(string $id): static
{
unset($this->blocks[$id]);
return $this;
}
Defined in: Yiisoft\View\State\StateTrait::removeParameter()
Removes a common parameter.
| public Yiisoft\View\State\WebViewState removeParameter ( string $id ) | ||
| $id | string |
The unique identifier of the parameter. |
public function removeParameter(string $id): static
{
unset($this->parameters[$id]);
return $this;
}
Defined in: Yiisoft\View\State\StateTrait::setBlock()
Sets a content block.
| public Yiisoft\View\State\WebViewState 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->blocks[$id] = $content;
return $this;
}
Defined in: Yiisoft\View\State\StateTrait::setParameter()
Sets a common parameter that is accessible in all view templates.
| public Yiisoft\View\State\WebViewState 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->parameters[$id] = $value;
return $this;
}
Defined in: Yiisoft\View\State\StateTrait::setParameters()
Sets a common parameters that is accessible in all view templates.
See also setParameter().
| public Yiisoft\View\State\WebViewState setParameters ( array $parameters ) | ||
| $parameters | array |
Parameters that are common for all view templates. |
public function setParameters(array $parameters): static
{
/** @var mixed $value */
foreach ($parameters as $id => $value) {
$this->setParameter($id, $value);
}
return $this;
}
Signup or Login in order to comment.