Class yii\bootstrap4\Toast
Inheritance | yii\bootstrap4\Toast » yii\bootstrap4\Widget » yii\base\Widget |
---|---|
Uses Traits | yii\bootstrap4\BootstrapWidgetTrait |
Source Code | https://github.com/yiisoft/yii2-bootstrap4/blob/master/src/Toast.php |
Toasts renders an toast bootstrap component.
For example,
echo Toast::widget([
'title' => 'Hello world!',
'dateTime' => 'now',
'body' => 'Say hello...',
]);
The following example will show the content enclosed between the begin() and end() calls within the toast box:
Toast::begin([
'title' => 'Hello world!',
'dateTime' => 'now'
]);
echo 'Say hello...';
Toast::end();
See also https://getbootstrap.com/docs/4.5/components/toasts/.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$body | string|null | The body content in the alert component. | yii\bootstrap4\Toast |
$bodyOptions | array | Body options | yii\bootstrap4\Toast |
$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 |
$closeButton | array | The options for rendering the close button tag. | yii\bootstrap4\Toast |
$dateTime | integer|string|DateTime|DateTimeInterface|DateInterval|false | The date time the toast message references to. | yii\bootstrap4\Toast |
$dateTimeOptions | array | Additional date time part options The following special options are supported: - tag: string, the tag name of the button. | yii\bootstrap4\Toast |
$headerOptions | array | Additional header options | yii\bootstrap4\Toast |
$options | array | The HTML attributes for the widget container tag. | yii\bootstrap4\Widget |
$title | string|null | The title content in the toast. | yii\bootstrap4\Toast |
$titleOptions | array | Additional title options The following special options are supported: - tag: string, the tag name of the button. | yii\bootstrap4\Toast |
Public Methods
Method | Description | Defined By |
---|---|---|
getView() | yii\bootstrap4\BootstrapWidgetTrait | |
init() | Initializes the widget. | yii\bootstrap4\Toast |
run() | yii\bootstrap4\Toast |
Protected Methods
Method | Description | Defined By |
---|---|---|
initOptions() | Initializes the widget options. | yii\bootstrap4\Toast |
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 |
renderBodyBegin() | Renders the opening tag of the toast body. | yii\bootstrap4\Toast |
renderBodyEnd() | Renders the toast body and the close button (if any). | yii\bootstrap4\Toast |
renderCloseButton() | Renders the close button. | yii\bootstrap4\Toast |
renderHeader() | Renders the header HTML markup of the modal | yii\bootstrap4\Toast |
Property Details
The body content in the alert component. Note that anything between the begin() and end() calls of the Toast widget will also be treated as the body content, and will be rendered before this.
Body options
See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
The options for rendering the close button tag. The close button is displayed in the header of the toast. Clicking on the button will hide the toast.
The following special options are supported:
- tag: string, the tag name of the button. Defaults to 'button'.
- label: string, the label of the button. Defaults to '×'.
The rest of the options will be rendered as the HTML attributes of the button tag. Please refer to the Toast documentation for the supported HTML attributes.
The date time the toast message references to.
This will be formatted as relative time (via formatter component). It will be omitted if
set to false
(default).
Additional date time part options
The following special options are supported:
- tag: string, the tag name of the button. Defaults to 'small'.
See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
Additional header options
See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
Additional title options
The following special options are supported:
- tag: string, the tag name of the button. Defaults to 'strong'.
See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
Method Details
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();
Initializes the widget.
This method will register the bootstrap asset bundle. If you override this method, make sure you call the parent implementation first.
public void init ( ) |
public function init()
{
parent::init();
$this->initOptions();
echo Html::beginTag('div', $this->options) . "\n";
echo $this->renderHeader() . "\n";
echo $this->renderBodyBegin() . "\n";
}
Initializes the widget options.
This method sets the default values for various options.
protected void initOptions ( ) |
protected function initOptions()
{
Html::addCssClass($this->options, ['widget' => 'toast']);
$this->closeButton = array_merge([
'aria' => ['label' => 'Close'],
'data' => ['dismiss' => 'toast'],
'class' => ['widget' => 'ml-2 mb-1 close'],
'type' => 'button'
], $this->closeButton);
if (!isset($this->options['role'])) {
$this->options['role'] = 'alert';
}
if (!isset($this->options['aria']) && !isset($this->options['aria-live'])) {
$this->options['aria'] = [
'live' => 'assertive',
'atomic' => 'true'
];
}
}
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));
}
}
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();
}
Renders the opening tag of the toast body.
protected string renderBodyBegin ( ) | ||
return | string |
The rendering result |
---|
protected function renderBodyBegin()
{
Html::addCssClass($this->bodyOptions, ['widget' => 'toast-body']);
return Html::beginTag('div', $this->bodyOptions);
}
Renders the toast body and the close button (if any).
protected string renderBodyEnd ( ) | ||
return | string |
The rendering result |
---|
protected function renderBodyEnd()
{
return $this->body . "\n" . Html::endTag('div');
}
Renders the close button.
protected string renderCloseButton ( ) | ||
return | string |
The rendering result |
---|
protected function renderCloseButton()
{
$tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
$label = ArrayHelper::remove($this->closeButton, 'label', Html::tag('span', '×', [
'aria-hidden' => 'true'
]));
return Html::tag($tag, "\n" . $label . "\n", $this->closeButton);
}
Renders the header HTML markup of the modal
protected string renderHeader ( ) | ||
return | string |
The rendering result |
---|
protected function renderHeader()
{
$button = $this->renderCloseButton();
$tag = ArrayHelper::remove($this->titleOptions, 'tag', 'strong');
Html::addCssClass($this->titleOptions, ['widget' => 'mr-auto']);
$title = Html::tag($tag, $this->title === null ? '' : $this->title, $this->titleOptions);
if ($this->dateTime !== false) {
$tag = ArrayHelper::remove($this->dateTimeOptions, 'tag', 'small');
Html::addCssClass($this->dateTimeOptions, ['widget' => 'text-muted']);
$title .= "\n" . Html::tag($tag, Yii::$app->formatter->asRelativeTime($this->dateTime), $this->dateTimeOptions);
}
$title .= "\n" . $button;
Html::addCssClass($this->headerOptions, ['widget' => 'toast-header']);
return Html::tag('div', "\n" . $title . "\n", $this->headerOptions);
}
public void run ( ) |
public function run()
{
echo "\n" . $this->renderBodyEnd();
echo "\n" . Html::endTag('div');
$this->registerPlugin('toast');
}