Final Class Yiisoft\Bootstrap5\Alert
| Inheritance | Yiisoft\Bootstrap5\Alert » Yiisoft\Widget\Widget |
|---|
Alert renders an alert bootstrap component.
For example,
<?= Alert::widget()->body('Say hello...')->variant(AlertVariant::PRIMARY) ?>
Public Methods
| Method | Description | Defined By |
|---|---|---|
| addAttributes() | Adds a sets of attributes. | Yiisoft\Bootstrap5\Alert |
| addClass() | Adds one or more CSS classes to the existing classes. | Yiisoft\Bootstrap5\Alert |
| addCloseButtonAttribute() | Adds close button attribute value. | Yiisoft\Bootstrap5\Alert |
| addCloseButtonClass() | Adds one or more CSS classes to the existing close button classes. | Yiisoft\Bootstrap5\Alert |
| addCloseButtonCssStyle() | Adds a close button CSS style. | Yiisoft\Bootstrap5\Alert |
| addCssStyle() | Adds a CSS style. | Yiisoft\Bootstrap5\Alert |
| attribute() | Adds a sets attribute value. | Yiisoft\Bootstrap5\Alert |
| attributes() | Sets the HTML attributes. | Yiisoft\Bootstrap5\Alert |
| body() | Sets the body content. | Yiisoft\Bootstrap5\Alert |
| class() | Replaces all existing CSS classes with the specified one(s). | Yiisoft\Bootstrap5\Alert |
| closeButtonAttributes() | Sets the HTML attributes for the close button. | Yiisoft\Bootstrap5\Alert |
| closeButtonLabel() | Sets the label for the close button. | Yiisoft\Bootstrap5\Alert |
| closeButtonTag() | Sets the HTML tag to be used for the close button. | Yiisoft\Bootstrap5\Alert |
| dismissable() | Makes the alert dismissible by adding a close button. | Yiisoft\Bootstrap5\Alert |
| fade() | Adds a fade effect. | Yiisoft\Bootstrap5\Alert |
| header() | Sets the header content. | Yiisoft\Bootstrap5\Alert |
| headerAttributes() | Sets the HTML attributes for the header section. | Yiisoft\Bootstrap5\Alert |
| headerTag() | Sets the HTML tag to be used for the header section. | Yiisoft\Bootstrap5\Alert |
| id() | Sets the ID. | Yiisoft\Bootstrap5\Alert |
| render() | Run the widget. | Yiisoft\Bootstrap5\Alert |
| templateContent() | Sets the template content to be used for rendering. | Yiisoft\Bootstrap5\Alert |
| variant() | Set the alert variant. | Yiisoft\Bootstrap5\Alert |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| CLASS_CLOSE_BUTTON | 'btn-close' | Yiisoft\Bootstrap5\Alert | |
| NAME | 'alert' | Yiisoft\Bootstrap5\Alert |
Method Details
Adds a sets of attributes.
| public self addAttributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. for example, |
| return | self |
A new instance with the specified attributes added. Example usage:
|
|---|---|---|
public function addAttributes(array $attributes): self
{
$new = clone $this;
$new->attributes = [...$this->attributes, ...$attributes];
return $new;
}
Adds one or more CSS classes to the existing classes.
Multiple classes can be added by passing them as separate arguments. null values are filtered out
automatically.
| public self addClass ( \BackedEnum|string|null $class ) | ||
| $class | \BackedEnum|string|null |
One or more CSS class names to add. Pass |
| return | self |
A new instance with the specified CSS classes added to existing ones. |
|---|---|---|
public function addClass(BackedEnum|string|null ...$class): self
{
$new = clone $this;
$new->cssClasses = [...$this->cssClasses, ...$class];
return $new;
}
Adds close button attribute value.
| public self addCloseButtonAttribute ( string $name, mixed $value ) | ||
| $name | string |
The attribute name. |
| $value | mixed |
The attribute value. |
| return | self |
A new instance with the specified attribute added. Example usage:
|
|---|---|---|
public function addCloseButtonAttribute(string $name, mixed $value): self
{
$new = clone $this;
$new->closeButtonAttributes[$name] = $value;
return $new;
}
Adds one or more CSS classes to the existing close button classes.
Multiple classes can be added by passing them as separate arguments. null values are filtered out
automatically.
| public self addCloseButtonClass ( \BackedEnum|string|null $class ) | ||
| $class | \BackedEnum|string|null |
One or more CSS class names to add. Pass |
| return | self |
A new instance with the specified CSS classes added to existing ones. |
|---|---|---|
public function addCloseButtonClass(BackedEnum|string|null ...$class): self
{
$new = clone $this;
foreach ($class as $item) {
Html::addCssClass($new->closeButtonAttributes, $item);
}
return $new;
}
Adds a close button CSS style.
| public self addCloseButtonCssStyle ( array|string $style, boolean $overwrite = true ) | ||
| $style | array|string |
The CSS style. If the value is an array, a space will separate the values.
For example, |
| $overwrite | boolean |
Whether to overwrite existing styles with the same name. If |
| return | self |
A new instance with the specified CSS style value added. Example usage:
// or
$alert->addCloseButtonCssStyle(['color' => 'red', 'font-weight' => 'bold']);
|
|---|---|---|
public function addCloseButtonCssStyle(array|string $style, bool $overwrite = true): self
{
$new = clone $this;
Html::addCssStyle($new->closeButtonAttributes, $style, $overwrite);
return $new;
}
Adds a CSS style.
| public self addCssStyle ( array|string $style, boolean $overwrite = true ) | ||
| $style | array|string |
The CSS style. If the value is an array, a space will separate the values.
For example, |
| $overwrite | boolean |
Whether to overwrite existing styles with the same name. If |
| return | self |
A new instance with the specified CSS style value added. Example usage:
// or
$alert->addCssStyle(['color' => 'red', 'font-weight' => 'bold']);
|
|---|---|---|
public function addCssStyle(array|string $style, bool $overwrite = true): self
{
$new = clone $this;
Html::addCssStyle($new->attributes, $style, $overwrite);
return $new;
}
Adds a sets attribute value.
| public self attribute ( string $name, mixed $value ) | ||
| $name | string |
The attribute name. |
| $value | mixed |
The attribute value. |
| return | self |
A new instance with the specified attribute added. Example usage:
|
|---|---|---|
public function attribute(string $name, mixed $value): self
{
$new = clone $this;
$new->attributes[$name] = $value;
return $new;
}
Sets the HTML attributes.
| public self attributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
A new instance with the specified attributes. |
|---|---|---|
public function attributes(array $attributes): self
{
$new = clone $this;
$new->attributes = $attributes;
return $new;
}
Sets the body content.
| public self body ( string|\Stringable $content, boolean $encode = true ) | ||
| $content | string|\Stringable |
The body content. |
| $encode | boolean |
Whether the body value should be HTML-encoded. Use this when rendering user-generated content to prevent XSS attacks. |
| return | self |
A new instance with the specified body content. Example usage:
|
|---|---|---|
public function body(string|Stringable $content, bool $encode = true): self
{
if ($encode) {
$content = Html::encode($content);
}
$new = clone $this;
$new->body = $content;
return $new;
}
Replaces all existing CSS classes with the specified one(s).
Multiple classes can be added by passing them as separate arguments. null values are filtered out
automatically.
| public self class ( \BackedEnum|string|null $class ) | ||
| $class | \BackedEnum|string|null |
One or more CSS class names to set. Pass |
| return | self |
A new instance with the specified CSS classes set. Example usage:
|
|---|---|---|
public function class(BackedEnum|string|null ...$class): self
{
$new = clone $this;
$new->cssClasses = $class;
return $new;
}
Sets the HTML attributes for the close button.
| public self closeButtonAttributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
A new instance with the specified HTML attributes for the close button. |
|---|---|---|
public function closeButtonAttributes(array $attributes): self
{
$new = clone $this;
$new->closeButtonAttributes = $attributes;
return $new;
}
Sets the label for the close button.
| public self closeButtonLabel ( string $label ) | ||
| $label | string |
The label for the close button. |
| return | self |
A new instance with the specified close button label. Example usage:
|
|---|---|---|
public function closeButtonLabel(string $label): self
{
$new = clone $this;
$new->closeButtonLabel = $label;
return $new;
}
Sets the HTML tag to be used for the close button.
| public self closeButtonTag ( string $tag ) | ||
| $tag | string |
The HTML tag name for the close button. |
| return | self |
A new instance with the specified close button tag. Example usage:
|
|---|---|---|
public function closeButtonTag(string $tag): self
{
$new = clone $this;
$new->closeButtonTag = $tag;
return $new;
}
Makes the alert dismissible by adding a close button.
| public self dismissable ( boolean $enabled ) | ||
| $enabled | boolean |
Whether to make the alert dismissible. |
| return | self |
A new instance with the specified dismissable value. Example usage:
|
|---|---|---|
public function dismissable(bool $enabled): self
{
$new = clone $this;
$new->dismissable = $enabled;
return $new;
}
Adds a fade effect.
| public self fade ( boolean $enabled ) | ||
| $enabled | boolean |
Whether to add a fade effect. |
| return | self |
A new instance with the specified fade value. Example usage:
|
|---|---|---|
public function fade(bool $enabled): self
{
$new = clone $this;
$new->fade = $enabled;
return $new;
}
Sets the header content.
| public self header ( string|null $content, boolean $encode = true ) | ||
| $content | string|null |
The header content. |
| $encode | boolean |
Whether the body value should be HTML-encoded. Use this when rendering user-generated content to prevent XSS attacks. |
| return | self |
A new instance with the specified header content. Example usage:
|
|---|---|---|
public function header(string|null $content, bool $encode = true): self
{
if ($encode) {
$content = Html::encode($content);
}
$new = clone $this;
$new->header = $content;
return $new;
}
Sets the HTML attributes for the header section.
| public self headerAttributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
A new instance with the specified attributes for the header section. |
|---|---|---|
public function headerAttributes(array $attributes): self
{
$new = clone $this;
$new->headerAttributes = $attributes;
return $new;
}
Sets the HTML tag to be used for the header section.
| public self headerTag ( string $tag ) | ||
| $tag | string |
The HTML tag name for the header section. |
| return | self |
A new instance with the specified header tag. Example usage:
|
|---|---|---|
public function headerTag(string $tag): self
{
$new = clone $this;
$new->headerTag = $tag;
return $new;
}
Sets the ID.
| public self id ( boolean|string $id ) | ||
| $id | boolean|string |
The ID of the component. If |
| return | self |
A new instance with the specified ID. Example usage:
|
|---|---|---|
public function id(bool|string $id): self
{
$new = clone $this;
$new->id = $id;
return $new;
}
Run the widget.
| public string render ( ) | ||
| return | string |
The HTML representation of the element. |
|---|---|---|
public function render(): string
{
$attributes = $this->attributes;
$attributes['role'] = self::NAME;
$toggler = '';
$classes = $attributes['class'] ?? null;
unset($attributes['class'], $attributes['id']);
Html::addCssClass($attributes, [self::NAME, $this->alertType->value, $classes, ...$this->cssClasses]);
if ($this->dismissable) {
Html::addCssClass($attributes, 'alert-dismissible');
$toggler = $this->renderToggler();
}
if ($this->fade) {
Html::addCssClass($attributes, 'fade show');
}
$content = strtr(
$this->templateContent,
[
'{header}' => $this->renderHeader(),
'{body}' => $this->body,
'{toggler}' => $toggler,
]
);
$content = preg_replace("/\n{2}/", "\n", $content) ?? '';
return Div::tag()->addAttributes($attributes)->content($content)->encode(false)->id($this->getId())->render();
}
Sets the template content to be used for rendering.
| public self templateContent ( string $content ) | ||
| $content | string |
The template content string. |
| return | self |
A new instance with the specified template content. Example usage:
|
|---|---|---|
public function templateContent(string $content): self
{
$new = clone $this;
$new->templateContent = $content;
return $new;
}
Set the alert variant.
| public self variant ( \Yiisoft\Bootstrap5\AlertVariant $variant ) | ||
| $variant | \Yiisoft\Bootstrap5\AlertVariant |
The alert variant. |
| return | self |
A new instance with the specified alert variant. Example usage:
|
|---|---|---|
public function variant(AlertVariant $variant): self
{
$new = clone $this;
$new->alertType = $variant;
return $new;
}
Signup or Login in order to comment.