Class yii\bootstrap4\Button

Inheritanceyii\bootstrap4\Button » yii\bootstrap4\Widget » yii\base\Widget
Uses Traitsyii\bootstrap4\BootstrapWidgetTrait
Source Code https://github.com/yiisoft/yii2-bootstrap4/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/4.5/components/buttons/.

Public Properties

Hide inherited properties

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

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers JS event handlers that are listed in $clientEvents. yii\bootstrap4\BootstrapWidgetTrait
registerPlugin() Registers a specific Bootstrap plugin and the related events yii\bootstrap4\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

getView() public abstract method

Defined in: yii\bootstrap4\BootstrapWidgetTrait::getView()

See also \yii\base\Widget::getView().

public abstract \yii\web\View getView ( )
return \yii\web\View

The view object that can be used to render views or view files.

                abstract function getView();

            
init() public method

Initializes the widget.

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

public void init ( )

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

            
registerClientEvents() protected method

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

Registers JS event handlers that are listed in $clientEvents.

protected void registerClientEvents ( )

                protected function registerClientEvents()
{
    if (!empty($this->clientEvents)) {
        $id = $this->options['id'];
        $js = [];
        foreach ($this->clientEvents as $event => $handler) {
            $js[] = "jQuery('#$id').on('$event', $handler);";
        }
        $this->getView()->registerJs(implode("\n", $js));
    }
}

            
registerPlugin() protected method

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

Registers a specific Bootstrap plugin and the related events

protected void registerPlugin ( $name )
$name string

The name of the Bootstrap plugin

                protected function registerPlugin($name)
{
    $view = $this->getView();
    BootstrapPluginAsset::register($view);
    $id = $this->options['id'];
    if ($this->clientOptions !== false) {
        $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
        $js = "jQuery('#$id').$name($options);";
        $view->registerJs($js);
    }
    $this->registerClientEvents();
}

            
run() public method

public void run ( )

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