Trait Yiisoft\View\State\StateTrait
| Implemented by | Yiisoft\View\State\ViewState, Yiisoft\View\State\WebViewState |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| 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 |
| getBlock() | Gets content of the block by ID. | Yiisoft\View\State\StateTrait |
| getParameter() | Gets a common parameter value by ID. | Yiisoft\View\State\StateTrait |
| getParameters() | Yiisoft\View\State\StateTrait | |
| 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 |
| 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 |
Method Details
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\StateTrait 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;
}
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.');
}
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.');
}
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]);
}
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]);
}
Removes a content block.
| public Yiisoft\View\State\StateTrait removeBlock ( string $id ) | ||
| $id | string |
The unique identifier of the block. |
public function removeBlock(string $id): static
{
unset($this->blocks[$id]);
return $this;
}
Removes a common parameter.
| public Yiisoft\View\State\StateTrait removeParameter ( string $id ) | ||
| $id | string |
The unique identifier of the parameter. |
public function removeParameter(string $id): static
{
unset($this->parameters[$id]);
return $this;
}
Sets a content block.
| public Yiisoft\View\State\StateTrait 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;
}
Sets a common parameter that is accessible in all view templates.
| public Yiisoft\View\State\StateTrait 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;
}
Sets a common parameters that is accessible in all view templates.
See also setParameter().
| public Yiisoft\View\State\StateTrait 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.