Skip to content

Core\Html::attributes

Writes 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. true writes the name alone. false and null leave the attribute out. A list is joined with spaces, and a style list is joined with ; .

Options

  • allowAnyUrlbooloptionalWith true, a URL attribute such as href is written with any scheme, including javascript:. The default is false. With false, a URL whose scheme is not http, https, mailto or tel leaves 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 with on, or is srcdoc`.
  • RuntimeErrorWhen an id, or one class in a class value, is empty or has a control character. An id and a class in a class list may not have a space. A RuntimeError is also thrown when a style value is not a list of CSS declarations, such as color: red.

Changelog

VersionChange
0.0.6Core\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";
Output
<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";
}
Output
<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 `&amp;`.
var $attrs = Core\Html::attributes($menu);
echo html`<button {$attrs}>Menu</button>`, "\n";
Output
<button type="button" data-menu-id="7" data-label="Tom &amp; Jerry" aria-expanded="false" aria-haspopup="true">Menu</button>