Skip to content
PHP Ecosystem
Explore

AttributesTrait

API reference › Bootstrap 6 › AttributesTrait

traityii2\extensions\bootstrap6\AttributesTrait
Sourcesrc/AttributesTrait.php
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.

MethodDescriptionDefined 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
public function addAttributes(array<string, mixed> $attributes): static

Adds a set of HTML attributes; attributes already set with the same name are replaced.

Parameters

ParameterTypeDescription
$attributesarray<string, mixed>Attribute values indexed by attribute names, for example ['id' => 'my-id'].

Returns

static

public function addClass(BackedEnum|string|null ...$class): static

Adds one or more CSS classes to the existing classes; duplicated classes are collapsed.

Parameters

ParameterTypeDescription
...$classBackedEnum|string|nullCSS class names to add; an enum case adds its value, and null is ignored.

Returns

static

See also

public function addCssStyle(array<string, string>|string $style, bool $overwrite = true): static

Adds 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

ParameterTypeDescription
$stylearray<string, string>|stringCSS style, either an array such as ['color' => 'red', 'font-weight' => 'bold'] or a string such as color: red; font-weight: bold;.
$overwriteboolWhether to replace the value of properties already set; when false, their existing value is kept.

Returns

static

See also

public function attribute(string $name, mixed $value): static

Sets an HTML attribute value.

Parameters

ParameterTypeDescription
$namestringAttribute name.
$valuemixedAttribute value, or null to omit the attribute when rendering.

Returns

static

public function class(BackedEnum|string|null ...$class): static

Replaces 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

ParameterTypeDescription
...$classBackedEnum|string|nullCSS 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