0 follower

Final Class Yiisoft\View\State\WebViewState

InheritanceYiisoft\View\State\WebViewState
Uses TraitsYiisoft\View\State\StateTrait

Public Methods

Hide inherited 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 Link tag. Yiisoft\View\State\WebViewState
registerMeta() Registers a meta tag. Yiisoft\View\State\WebViewState
registerMetaTag() Registers a Meta tag. Yiisoft\View\State\WebViewState
registerScriptTag() Register a script tag Yiisoft\View\State\WebViewState
registerStyleTag() Register a 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

Hide inherited methods

addCssFiles() public method

It processes the CSS configuration generated by the asset manager and converts it into HTML code.

public addCssFiles( array $cssFiles ): void
$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],
        );
    }
}

            
addCssStrings() public method

It processes the CSS strings generated by the asset manager.

public addCssStrings( array $cssStrings ): void
$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],
        );
    }
}

            
addJsFiles() public method

It processes the JS configuration generated by the asset manager and converts it into HTML code.

public addJsFiles( array $jsFiles ): void
$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],
        );
    }
}

            
addJsStrings() public method

It processes the JS strings generated by the asset manager.

public addJsStrings( array $jsStrings ): void
$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],
        );
    }
}

            
addJsVars() public method

It processes the JS variables generated by the asset manager and converts it into JS code.

public addJsVars( array $jsVars ): void
$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);
        }
    }
}

            
addToParameter() public method

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 addToParameter( string $id, mixed $value ): Yiisoft\View\State\WebViewState
$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;
}

            
clear() public method

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 clear( ): void

                public function clear(): void
{
    $this->parameters = [];
    $this->blocks = [];
    $this->title = '';
    $this->metaTags = [];
    $this->linkTags = [];
    $this->css = [];
    $this->cssFiles = [];
    $this->js = [];
    $this->jsFiles = [];
}

            
getBlock() public method

Defined in: Yiisoft\View\State\StateTrait::getBlock()

Gets content of the block by ID.

public getBlock( string $id ): string
$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.');
}

            
getCss() public method

public getCss( ): array
return array

The registered CSS code blocks.

                public function getCss(): array
{
    return $this->css;
}

            
getCssFiles() public method

public getCssFiles( ): array
return array

The registered CSS files.

                public function getCssFiles(): array
{
    return $this->cssFiles;
}

            
getJs() public method

public getJs( ): array
return array

The registered JS code blocks

                public function getJs(): array
{
    return $this->js;
}

            
getJsFiles() public method

public getJsFiles( ): array
return array

The registered JS files.

                public function getJsFiles(): array
{
    return $this->jsFiles;
}

            
getLinkTags() public method

public getLinkTags( ): array
return array

The registered link tags.

                public function getLinkTags(): array
{
    return $this->linkTags;
}

            
getMetaTags() public method

public getMetaTags( ): \Yiisoft\Html\Tag\Meta[]
return \Yiisoft\Html\Tag\Meta[]

The registered meta tags.

                public function getMetaTags(): array
{
    return $this->metaTags;
}

            
getParameter() public method

Defined in: Yiisoft\View\State\StateTrait::getParameter()

Gets a common parameter value by ID.

public getParameter( string $id, mixed $default ): mixed
$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.');
}

            
getParameters() public method
public getParameters( ): array

                public function getParameters(): array
{
    return $this->parameters;
}

            
getTitle() public method

Get title in views.

public getTitle( ): string

                public function getTitle(): string
{
    return $this->title;
}

            
hasBlock() public method

Defined in: Yiisoft\View\State\StateTrait::hasBlock()

Checks the existence of a content block by ID.

public hasBlock( string $id ): boolean
$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]);
}

            
hasParameter() public method

Defined in: Yiisoft\View\State\StateTrait::hasParameter()

Checks the existence of a common parameter by ID.

public hasParameter( string $id ): boolean
$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]);
}

            
registerCss() public method

Registers a CSS code block.

public registerCss( string $css, integer $position WebView::POSITION_HEAD, array $attributes = [], string|null $key null ): void
$css string

The content of the CSS code block to be registered.

$position integer
$attributes array

The HTML attributes for the \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 = WebView::POSITION_HEAD,
    array $attributes = [],
    ?string $key = null,
): void {
    $this->css[$position][$key ?? md5($css)] = $attributes === [] ? $css : Html::style($css, $attributes);
}

            
registerCssFile() public method

Registers a CSS file.

This method should be used for simple registration of CSS files. If you want to use features of \Yiisoft\Assets\AssetManager like appending timestamps to the URL and file publishing options, use \Yiisoft\Assets\AssetBundle.

public registerCssFile( string $url, integer $position WebView::POSITION_HEAD, array $options = [], string|null $key null ): void
$url string

The CSS file to be registered.

$position integer
$options array

The HTML attributes for the link tag. Please refer to \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 = 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();
}

            
registerCssFromFile() public method

Registers a CSS code block from file.

See also registerCss().

public registerCssFromFile( string $path, integer $position WebView::POSITION_HEAD, array $attributes = [], string|null $key null ): void
$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);
}

            
registerJs() public method

Registers a JS code block.

public registerJs( string $js, integer $position WebView::POSITION_END, string|null $key null ): void
$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 = WebView::POSITION_END, ?string $key = null): void
{
    $this->js[$position][$key ?? md5($js)] = $js;
}

            
registerJsFile() public method

Registers a JS file.

This method should be used for simple registration of JS files. If you want to use features of \Yiisoft\Assets\AssetManager like appending timestamps to the URL and file publishing options, use \Yiisoft\Assets\AssetBundle.

public registerJsFile( string $url, integer $position WebView::POSITION_END, array $options = [], string|null $key null ): void
$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 \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();
}

            
registerJsVar() public method

Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names.

public registerJsVar( string $name, mixed $value, integer $position WebView::POSITION_HEAD ): void
$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);
}

            
registerLink() public method

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 registerCssFile()] instead, which has more options for this kind of link tag.

public registerLink( array $attributes, integer $position WebView::POSITION_HEAD, string|null $key null ): void
$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.

registerLinkTag() public method

Registers a Link tag.

See also registerLink().

public registerLinkTag( \Yiisoft\Html\Tag\Link $link, integer $position WebView::POSITION_HEAD, string|null $key null ): void
$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;
}

            
registerMeta() public method

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 registerMeta( array $attributes, string|null $key null ): void
$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);
}

            
registerMetaTag() public method

Registers a Meta tag.

See also registerMeta().

public registerMetaTag( \Yiisoft\Html\Tag\Meta $meta, string|null $key null ): void
$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;
}

            
registerScriptTag() public method

Register a script tag

See also registerJs().

public registerScriptTag( \Yiisoft\Html\Tag\Script $script, integer $position WebView::POSITION_END, string|null $key null ): void
$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;
}

            
registerStyleTag() public method

Register a Style tag.

See also registerJs().

public registerStyleTag( \Yiisoft\Html\Tag\Style $style, integer $position WebView::POSITION_HEAD, string|null $key null ): void
$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;
}

            
removeBlock() public method

Defined in: Yiisoft\View\State\StateTrait::removeBlock()

Removes a content block.

public removeBlock( string $id ): Yiisoft\View\State\WebViewState
$id string

The unique identifier of the block.

                public function removeBlock(string $id): static
{
    unset($this->blocks[$id]);
    return $this;
}

            
removeParameter() public method

Defined in: Yiisoft\View\State\StateTrait::removeParameter()

Removes a common parameter.

public removeParameter( string $id ): Yiisoft\View\State\WebViewState
$id string

The unique identifier of the parameter.

                public function removeParameter(string $id): static
{
    unset($this->parameters[$id]);
    return $this;
}

            
setBlock() public method

Defined in: Yiisoft\View\State\StateTrait::setBlock()

Sets a content block.

public setBlock( string $id, string $content ): Yiisoft\View\State\WebViewState
$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;
}

            
setParameter() public method

Defined in: Yiisoft\View\State\StateTrait::setParameter()

Sets a common parameter that is accessible in all view templates.

public setParameter( string $id, mixed $value ): Yiisoft\View\State\WebViewState
$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;
}

            
setParameters() public method

Defined in: Yiisoft\View\State\StateTrait::setParameters()

Sets a common parameters that is accessible in all view templates.

See also setParameter().

public setParameters( array $parameters ): Yiisoft\View\State\WebViewState
$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;
}

            
setTitle() public method

Set title in views.

getTitle()

public setTitle( string $value ): self
$value string

                public function setTitle(string $value): self
{
    $this->title = $value;
    return $this;
}