Skip to content

Kirby 4.2.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']);             // selected="selected"
Xml::attr('selected', true);         // selected="selected"
Xml::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
Xml::attr('class', $page->classString()->or(null)->value());

// 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"