AttributesTrait
API reference › Bootstrap 6 › AttributesTrait
Used by 2 types
Provides fluent methods that modify the HTML attributes of the widget element.
The using class must declare a public $options array; every method modifies it in place and returns the same
instance, so calls chain on the value returned by begin(), new, or Yii::createObject(). The classes a widget
requires (for example collapse or navbar) are added while rendering, from a copy of $options, so these methods
never remove them.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
addAttributes() | Adds a set of HTML attributes; attributes already set with the same name are replaced. | AttributesTrait |
addClass() | Adds one or more CSS classes to the existing classes; duplicated classes are collapsed. | AttributesTrait |
addCssStyle() | Adds CSS style properties to the style attribute. | AttributesTrait |
attribute() | Sets an HTML attribute value. | AttributesTrait |
class() | Replaces the existing CSS classes with the specified ones. | AttributesTrait |
Method details
Section titled “Method details”addAttributes()
Section titled “addAttributes() ”public function addAttributes(array<string, mixed> $attributes): staticAdds a set of HTML attributes; attributes already set with the same name are replaced.
Parameters
| Parameter | Type | Description |
|---|---|---|
$attributes | array<string, | Attribute values indexed by attribute names, for example ['id' => 'my-id']. |
Returns
static
addClass()
Section titled “addClass() ”public function addClass(BackedEnum|string|null ...$class): staticAdds one or more CSS classes to the existing classes; duplicated classes are collapsed.
Parameters
| Parameter | Type | Description |
|---|---|---|
...$class | BackedEnum| | CSS class names to add; an enum case adds its value, and null is ignored. |
Returns
static
See also
addCssStyle()
Section titled “addCssStyle() ”public function addCssStyle(array<string, string>|string $style, bool $overwrite = true): staticAdds CSS style properties to the style attribute.
Without an existing style, a string is stored as is and an array renders as color: red;. With an existing
style, both are merged and normalized to name: value; pairs: new properties are appended, and a property
already set keeps its position.
Parameters
| Parameter | Type | Description |
|---|---|---|
$style | array<string, | CSS style, either an array such as ['color' => 'red', or a string such as color: red; font-weight: bold;. |
$overwrite | bool | Whether to replace the value of properties already set; when false, their existing value is kept. |
Returns
static
See also
attribute()
Section titled “attribute() ”public function attribute(string $name, mixed $value): staticSets an HTML attribute value.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | Attribute name. |
$value | mixed | Attribute value, or null to omit the attribute when rendering. |
Returns
static
class()
Section titled “class() ”public function class(BackedEnum|string|null ...$class): staticReplaces the existing CSS classes with the specified ones.
Only the classes held in $options are replaced; the classes the widget requires are still added while
rendering.
Parameters
| Parameter | Type | Description |
|---|---|---|
...$class | BackedEnum| | CSS class names to set; an enum case sets its value, and null is ignored. When no class remains, the class attribute is removed. |
Returns
static
See also