0 follower

Abstract Class Yiisoft\Widget\Widget

InheritanceYiisoft\Widget\Widget
ImplementsYiisoft\Html\NoEncodeStringableInterface

Widget generates a string content based on some logic and input data.

These are typically used in templates to conceal complex HTML rendering logic.

This is the base class that is meant to be inherited when implementing your own widgets.

Public Methods

Hide inherited methods

Method Description Defined By
__toString() Allows not to call ->render() explicitly: Yiisoft\Widget\Widget
begin() Used to open a wrapping widget (the one with begin/end). Yiisoft\Widget\Widget
end() Checks that the widget was opened with {@see begin()}. If so, runs it and returns content generated. Yiisoft\Widget\Widget
render() Renders widget content. Yiisoft\Widget\Widget
widget() Creates a widget instance. Yiisoft\Widget\Widget

Protected Methods

Hide inherited methods

Method Description Defined By
getThemeConfig() Returns configuration that will be merged with configuration passed to {@see widget()} method. Yiisoft\Widget\Widget

Method Details

Hide inherited methods

__toString() public method

Allows not to call ->render() explicitly:

<?= MyWidget::widget(); ?>
public string __toString ( )

                final public function __toString(): string
{
    return $this->render();
}

            
begin() public method

Used to open a wrapping widget (the one with begin/end).

When implementing this method, don't forget to call parent::begin().

public string|null begin ( )
return string|null

Opening part of widget markup.

                public function begin(): ?string
{
    self::$stack[] = $this;
    return null;
}

            
end() public static method

Checks that the widget was opened with {@see begin()}. If so, runs it and returns content generated.

public static string end ( )
throws RuntimeException

                final public static function end(): string
{
    if (self::$stack === []) {
        throw new RuntimeException(sprintf(
            'Unexpected "%s::end()" call. A matching "%s::begin()" is not found.',
            static::class,
            static::class,
        ));
    }
    $widget = array_pop(self::$stack);
    $widgetClass = $widget::class;
    if ($widgetClass !== static::class) {
        throw new RuntimeException(sprintf(
            'Expecting "%s::end()" call, found "%s::end()".',
            $widgetClass,
            static::class,
        ));
    }
    return $widget->render();
}

            
getThemeConfig() protected static method

Returns configuration that will be merged with configuration passed to {@see widget()} method.

protected static array getThemeConfig ( string|null $theme )
$theme string|null

The widget theme.

return array

Configuration in the form of array definition (see syntax description in the Yii Definitions documentation by link {@link https://github.com/yiisoft/definitions#arraydefinition}).

                protected static function getThemeConfig(?string $theme): array
{
    return [];
}

            
render() public abstract method

Renders widget content.

This method must be overridden when implementing concrete widget.

public abstract string render ( )
return string

The result of widget execution to be outputted.

                abstract public function render(): string;

            
widget() public static method

Creates a widget instance.

public static Yiisoft\Widget\Widget widget ( array $constructorArguments = [], array $config = [], string|null $theme null )
$constructorArguments array

The constructor arguments.

$config array

The configuration for creating a widget. For a description of the configuration syntax, see array definitions documentation in the Yii Definitions by link {@link https://github.com/yiisoft/definitions#arraydefinition).

$theme string|null

The widget theme.

return Yiisoft\Widget\Widget

The widget instance.

throws \Yiisoft\Definitions\Exception\InvalidConfigException
throws \Yiisoft\Definitions\Exception\CircularReferenceException
throws \Yiisoft\Definitions\Exception\NotInstantiableException
throws \Yiisoft\Factory\NotFoundException

                final public static function widget(
    array $constructorArguments = [],
    array $config = [],
    ?string $theme = null
): static {
    $config = ArrayDefinitionHelper::merge(
        static::getThemeConfig($theme),
        $config,
        empty($constructorArguments) ? [] : [ArrayDefinition::CONSTRUCTOR => $constructorArguments],
    );
    $config[ArrayDefinition::CLASS_NAME] = static::class;
    return WidgetFactory::createWidget($config, $theme);
}