Final Class Yiisoft\Yii\Widgets\Block
| Inheritance | Yiisoft\Yii\Widgets\Block » Yiisoft\Widget\Widget |
|---|
Block records all output between {@see Widget::begin()} and {@see Widget::end()} calls and stores it in.
The general idea is that you're defining block default in a view or layout:
<?php Block::widget()
->id('my-block')
->begin() ?>
Nothing.
<?php Block::end() ?>
And then overriding default in views:
<?php Block::widget()
->id('my-block')
->begin() ?>
Umm... hello?
<?php Block::end() ?>
in subviews show block:
<?= $this->getBlock('my-block') ?>
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Yii\Widgets\Block | |
| begin() | Starts recording a block. | Yiisoft\Yii\Widgets\Block |
| id() | Returns a new instance with the specified Widget ID. | Yiisoft\Yii\Widgets\Block |
| render() | Ends recording a block. | Yiisoft\Yii\Widgets\Block |
| renderInPlace() | Enables in-place rendering and returns a new instance. | Yiisoft\Yii\Widgets\Block |
Method Details
| public mixed __construct ( \Yiisoft\View\WebView $webView ) | ||
| $webView | \Yiisoft\View\WebView | |
public function __construct(private WebView $webView)
{
}
Starts recording a block.
| public string|null begin ( ) |
public function begin(): string|null
{
parent::begin();
ob_start();
return null;
}
Returns a new instance with the specified Widget ID.
| public self id ( string $value ) | ||
| $value | string |
The Widget ID. |
public function id(string $value): self
{
$new = clone $this;
$new->id = $value;
return $new;
}
Ends recording a block.
This method stops output buffering and saves the rendering result as a named block in the view.
| public string render ( ) | ||
| return | string |
The result of widget execution to be outputted. |
|---|---|---|
public function render(): string
{
if ($this->id === '') {
ob_end_clean();
throw new RuntimeException('You must assign the "id" using the "id()" setter.');
}
$block = ob_get_clean();
if ($block === false || $block === '') {
return '';
}
if ($this->renderInPlace) {
return $block;
}
$this->webView->setBlock($this->id, $block);
return '';
}
Enables in-place rendering and returns a new instance.
Without calling this method, the captured content of the block is not displayed.
| public self renderInPlace ( ) |
public function renderInPlace(): self
{
$new = clone $this;
$new->renderInPlace = true;
return $new;
}
Signup or Login in order to comment.