Skip to content

Kirby 4.4.1

Html::attr()

Generates a single attribute or a list of attributes

Html::attr(array|string $name, mixed $value = null, ?string $before = null, ?string $after = null): string|null

Parameters

Name Type Default Description
$name * array|string String: A single attribute with that name will be generated.
Key-value array: A list of attributes will be generated. Don't pass a second argument in that case.
$value mixed null If used with a $name string, pass the value of the attribute here.
If used with a $name array, this can be set to false to disable attribute sorting.
$before string|null null An optional string that will be prepended if the result is not empty
$after string|null null An optional string that will be appended if the result is not empty

Return type

string|null

Parent class

Kirby\Toolkit\Html

Examples

// single attributes
Html::attr('style', 'color: red'); // style="color: red"
Html::attr('selected', true);      // selected
Html::attr('class', ['a', 'b']);   // class="a b"

// single attribute from a field (that can possibly be empty);
// the attribute will be omitted if the field is empty
Html::attr('class', $page->classString()->or(null)->value());

// multiple attributes
Html::attr(['style' => 'color: red', 'selected' => true]); // style="color: red" selected
Html::attr(['style' => 'color: red', 'selected']);         // style="color: red" selected

// multiple attributes without sorting
Html::attr(['style' => 'color: red', 'disabled'], false); // style="color: red" disabled