Xml::attr()
Generates a single attribute or a list of attributes
Xml::attr(string|array $name, mixed $value = null): string|nullParameters
| 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 $namestring, pass the value of the attribute here.If used with a $namearray, this can be set tofalseto disable attribute sorting. | 
Return types
stringornull
The generated XML attributes string
Parent class
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"