Skip to content

Kirby 5.0.4

Html::attr()

Generates a single attribute or a list of attributes

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

Parameters

Name Type Default Description
$namerequired stringorarray no default value 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 stringornull null An optional string that will be prepended if the result is not empty
$after stringornull null An optional string that will be appended if the result is not empty

Return types

stringornull

The generated HTML attributes string

Parent class

Kirby\Cms\Html inherited from 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