Skip to content

Kirby 3.9.0

Xml::attr()

Generates a single attribute or a list of attributes

Xml::attr(array|string $name, mixed $value = 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.

Return type

string|null

Parent class

Kirby\Toolkit\Xml

Examples

// single attributes
Xml::attr('viewBox', '0 0 100 100'); // viewBox="0 0 100 100"
Xml::attr('selected', true);         // selected="selected"
Xml::attr('class', ['a', 'b']);      // class="a b"

// multiple attributes
Xml::attr(['viewBox' => '0 0 100 100', 'selected' => true]); // viewBox="0 0 100 100" selected="selected"
Xml::attr(['viewBox' => '0 0 100 100', 'selected']);         // viewBox="0 0 100 100" selected="selected"

// multiple attributes without sorting
Xml::attr(['viewBox' => '0 0 100 100', 'stroke' => 'red'], false); // viewBox="0 0 100 100" stroke="red"
New in 3.9.0

Support for the Xml::attr(['selected']) syntax without value was added in Kirby 3.9.0.