Core\Html::attributes
Core\Html::attributes(
array<?string|int|uint|float|bool|array<?string>> $attributes,
{allowAnyUrl?: bool},
): Core\Html\MarkupWrites the attributes of an HTML tag from a map.
Core\Html::attributes takes a map from attribute name to value. It writes each attribute as name="value", with one space between two attributes. You put the result after the tag name and a space: ` html<input {$attrs}> `. Every value is escaped and written in double quotes.
true writes the name alone, such as required. false and null leave the attribute out. A list is joined with spaces, so "class" => ["btn", "big"] writes class="btn big". For an aria-* attribute, true and false write the words "true" and "false".
A URL attribute, such as href, is checked. If its scheme (the part before the first :) is not http, https, mailto or tel, the attribute is left out. So a javascript: link from a visitor is never written.
A name that starts with on, such as onclick, throws a LogicError. The value of such an attribute runs as JavaScript, and escaping does not make it safe.
Good to know: if you write a name and a value directly in the code and they would throw, the program does not compile. A value from a variable is checked when the call runs.
Parameters
$attributesarray<?string|int|uint|float|bool|array<?string>>The attributes, from name to value, in the order to write them.truewrites the name alone.falseandnullleave the attribute out. A list is joined with spaces, and astylelist is joined with;.
Options
allowAnyUrlbooloptionalWithtrue, a URL attribute such ashrefis written with any scheme, includingjavascript:. The default isfalse. Withfalse, a URL whose scheme is nothttp,https,mailtoortelleaves its attribute out.
Return value
Returns Core\Html\Markup. A Core\Html\Markup to write after a tag name and a space, as in `html<input {$attrs}> `. An empty map gives empty markup.
Errors
LogicErrorWhen a name is empty, has a space, a control character or one of" ' < > / =`, or starts withon, or issrcdoc`.RuntimeErrorWhen anid, or one class in aclassvalue, is empty or has a control character. Anidand a class in aclasslist may not have a space. ARuntimeErroris also thrown when astylevalue is not a list of CSS declarations, such ascolor: red.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Html::attributes writes a map of attributes into a tag, such as <input<?= Core\Html::attributes(['value' => $name, 'disabled' => true]) ?>>. Every value is escaped and quoted, true writes the name alone, and false and null leave the attribute out. A URL attribute such as href is left out when its URL starts with javascript:. |
Examples
A form input required and disabled
<?nvs// Shows how to write the attributes of a form field from one map.
class Form{ public static function emailField(bool $locked): Core\Html\Markup { // `true` writes the name alone. `false` leaves the attribute out. var $attrs = Core\Html::attributes([ 'type' => 'email', 'name' => 'email', 'required' => true, 'disabled' => $locked, ]); return html`<input {$attrs}>`; }}
// With `true`, this prints `<input type="email" name="email" required disabled>`.echo Form::emailField(true), "\n";
// With `false`, this prints `<input type="email" name="email" required>`.echo Form::emailField(false), "\n";<input type="email" name="email" required disabled><input type="email" name="email" required>A link from a visitor
<?nvs// Shows how to write a link whose address a visitor typed, such as the website on a profile.
array<string> $websites = ['https://example.com/blog', 'javascript:alert(1)'];
// Each address is read as `tainted`, which means it came from outside the program.foreach ($websites as tainted string $website) { // An `https` address is written. A `javascript:` address leaves `href` out. var $attrs = Core\Html::attributes(['href' => $website, 'rel' => 'nofollow']); echo html`<a {$attrs}>Website</a>`, "\n";}<a href="https://example.com/blog" rel="nofollow">Website</a><a rel="nofollow">Website</a>Data and aria from a map
<?nvs// Shows how to write `data-*` and `aria-*` attributes from a map that the program builds.
array<string|int|bool> $menu = [ 'type' => 'button', 'data-menu-id' => 7, 'data-label' => 'Tom & Jerry',];
// An `aria-*` attribute writes `true` and `false` as words.$menu['aria-expanded'] = false;$menu['aria-haspopup'] = true;
// The attributes are written in the order of the map. The `&` is written as `&`.var $attrs = Core\Html::attributes($menu);echo html`<button {$attrs}>Menu</button>`, "\n";<button type="button" data-menu-id="7" data-label="Tom & Jerry" aria-expanded="false" aria-haspopup="true">Menu</button>