Class yii\bootstrap5\Button

Inheritanceyii\bootstrap5\Button » yii\bootstrap5\Widget » yii\base\Widget
Uses Traitsyii\bootstrap5\BootstrapWidgetTrait
Source Code https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/Button.php

Button renders a bootstrap button.

For example,

echo Button::widget([
    'label' => 'Action',
    'options' => ['class' => 'btn-lg'],
]);

See also https://getbootstrap.com/docs/5.1/components/buttons/.

Public Properties

Hide inherited properties

Property Type Description Defined By
$clientEvents array The event handlers for the underlying Bootstrap JS plugin. yii\bootstrap5\BootstrapWidgetTrait
$clientOptions array|false The options for the underlying Bootstrap JS plugin/component. yii\bootstrap5\BootstrapWidgetTrait
$encodeLabel boolean Whether the label should be HTML-encoded. yii\bootstrap5\Button
$label string The button label yii\bootstrap5\Button
$options array The HTML attributes for the widget container tag. yii\bootstrap5\Widget
$tagName string The tag to use to render the button yii\bootstrap5\Button

Public Methods

Hide inherited methods

Method Description Defined By
init() Initializes the widget. yii\bootstrap5\Button
run() yii\bootstrap5\Button

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers JS event handlers that are listed in $clientEvents. yii\bootstrap5\BootstrapWidgetTrait
registerPlugin() Registers a specific Bootstrap plugin/component and the related events. yii\bootstrap5\BootstrapWidgetTrait

Property Details

Hide inherited properties

$encodeLabel public property

Whether the label should be HTML-encoded.

public boolean $encodeLabel true
$label public property

The button label

public string $label 'Button'
$tagName public property

The tag to use to render the button

public string $tagName 'button'

Method Details

Hide inherited methods

init() public method

Initializes the widget.

If you override this method, make sure you call the parent implementation first.

public void init ( )
throws \yii\base\InvalidConfigException

                public function init(): void
{
    parent::init();
    Html::addCssClass($this->options, [
        'widget' => 'btn',
    ]);
}

            
registerClientEvents() protected method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::registerClientEvents()

Registers JS event handlers that are listed in $clientEvents.

protected void registerClientEvents ( ?string $name null )
$name ?string

                protected function registerClientEvents(?string $name = null): void
{
    if (!empty($this->clientEvents)) {
        $id = $this->options['id'];
        $js = [];
        $appendix = ($name === 'dropdown') ? '.parentElement' : '';
        foreach ($this->clientEvents as $event => $handler) {
            $js[] = "document.getElementById('$id')$appendix.addEventListener('$event', $handler);";
        }
        $this->getView()->registerJs(implode("\n", $js));
    }
}

            
registerPlugin() protected method

Defined in: yii\bootstrap5\BootstrapWidgetTrait::registerPlugin()

Registers a specific Bootstrap plugin/component and the related events.

protected void registerPlugin ( string $name )
$name string

The name of the Bootstrap plugin

                protected function registerPlugin(string $name): void
{
    /**
     * @see https://github.com/twbs/bootstrap/blob/v5.2.0/js/index.esm.js
     */
    $jsPlugins = [
        'alert',
        'button',
        'carousel',
        'collapse',
        'dropdown',
        'modal',
        'offcanvas',
        'popover',
        'scrollspy',
        'tab',
        'toast',
        'tooltip',
    ];
    if (in_array($name, $jsPlugins, true)) {
        $view = $this->getView();
        BootstrapPluginAsset::register($view);
        // 'popover', 'toast' and 'tooltip' plugins not activates via data attributes
        if ($this->clientOptions !== false || in_array($name, ['popover', 'toast', 'tooltip'], true)) {
            $name = ucfirst($name);
            $id = $this->options['id'];
            $options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
            $view->registerJs("(new bootstrap.$name('#$id', $options));");
        }
        $this->registerClientEvents($name);
    }
}

            
run() public method

public string run ( )

                public function run(): string
{
    $this->registerPlugin('button');
    return Html::tag(
        $this->tagName,
        $this->encodeLabel ? Html::encode($this->label) : $this->label,
        $this->options,
    );
}