0 follower

Final Class Yiisoft\Yii\Widgets\ContentDecorator

InheritanceYiisoft\Yii\Widgets\ContentDecorator » Yiisoft\Widget\Widget

ContentDecorator records all output between {@see Widget::begin()} and {@see Widget::end()} calls, passes it to the given view file as $content and then echoes rendering result.

<?= ContentDecorator::widget()
    ->viewFile('@app/views/layouts/base.php')
    ->parameters([])
    ->begin();
?>

some content here

<?= ContentDecorator::end() ?>

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Yii\Widgets\ContentDecorator
begin() Starts recording a content. Yiisoft\Yii\Widgets\ContentDecorator
parameters() Returns a new instance with the specified parameters. Yiisoft\Yii\Widgets\ContentDecorator
render() Ends recording a content. Yiisoft\Yii\Widgets\ContentDecorator
view() Returns a new instance with the specified view instance. Yiisoft\Yii\Widgets\ContentDecorator
viewFile() Returns a new instance with the specified view file. Yiisoft\Yii\Widgets\ContentDecorator

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Aliases\Aliases $aliases, \Yiisoft\View\ViewInterface $webView )
$aliases \Yiisoft\Aliases\Aliases
$webView \Yiisoft\View\ViewInterface

                public function __construct(private readonly Aliases $aliases, ViewInterface $webView)
{
    $this->view = $webView;
}

            
begin() public method

Starts recording a content.

public ?string begin ( )

                public function begin(): ?string
{
    parent::begin();
    ob_start();
    return null;
}

            
parameters() public method

Returns a new instance with the specified parameters.

public self parameters ( array $value )
$value array

The parameters (name => value) to be extracted and made available in the decorative view.

                public function parameters(array $value): self
{
    $new = clone $this;
    $new->parameters = $value;
    return $new;
}

            
render() public method

Ends recording a content.

This method stops output buffering and saves the rendering result as a $content variable and then echoes rendering result.

public string render ( )
return string

The result of widget execution to be outputted.

throws \Throwable\Yiisoft\View\Exception\ViewNotFoundException

                public function render(): string
{
    $parameters = $this->parameters;
    $parameters['content'] = ob_get_clean();
    return $this->view->render($this->viewFile, $parameters);
}

            
view() public method

Returns a new instance with the specified view instance.

Passing the view instance is needed to use current state (e.g., parameters) in the decorator view file.

public self view ( \Yiisoft\View\ViewInterface $view, string $viewFile '' )
$view \Yiisoft\View\ViewInterface

The view instance ({@see \Yiisoft\Yii\Widgets\View} or {@see \Yiisoft\View\WebView}) to use for rendering.

$viewFile string

The view file that will be used to decorate the content enclosed by this widget. This can be specified as either the view file path or alias path. If empty, the view file set via {@see \Yiisoft\Yii\Widgets\viewFile()} will be used.

                public function view(ViewInterface $view, string $viewFile = ''): self
{
    $new = clone $this;
    $new->view = $view;
    if ($viewFile !== '') {
        $new->viewFile = $this->aliases->get($viewFile);
    }
    return $new;
}

            
viewFile() public method

Returns a new instance with the specified view file.

public self viewFile ( string $value )
$value string

The view file that will be used to decorate the content enclosed by this widget. This can be specified as either the view file path or alias path.

                public function viewFile(string $value): self
{
    $new = clone $this;
    $new->viewFile = $this->aliases->get($value);
    return $new;
}