{attrs}

Writes HTML attributes from values - quoted and escaped, a true boolean attribute bare, a false one left out.

Text and formatting Single tags

Syntax

{attrs name=$value, other=$value}
{attrs required, aria-expanded=$open}
{attrs $array}

Parameters

items
Any number of items: name=expression (any HTML attribute name, dashes included), a bare name written as a bare attribute, or an expression whose array value adds its keys as attributes.

Description

{attrs} stands inside an HTML start tag and writes the attributes its items describe, separated by spaces: <button {attrs disabled=$busy, title=$help}>. Each value is escaped for the attribute, so a quote in a value cannot end it. The items are read raw - content=, data= or print= are attribute names here, not the options they are on other tags.

True, false and the rest

A boolean HTML attribute - disabled, checked, selected, required, hidden, readonly and the like - is written bare when its value is true and left out otherwise. Any other attribute is left out for FALSE and NULL, written bare for TRUE, and otherwise written with its value: the text 'false' stays aria-expanded="false", an empty text is an empty attribute, an array is joined with spaces. A name alone - {attrs required} - is a bare attribute.

Attributes from an array

An item without a name whose value is an array adds its keys as attributes. Those keys are data, often a visitor's, so some are left out: a key that is no attribute name, an event handler (on...), a script library's directive in any spelling (@, :, x-, v-, hx-, ng-, wire: and more), an iframe's srcdoc, and a URL attribute whose value names a scheme that runs script (javascript:, vbscript:, data: other than data:image/).

For the class attribute, {classes} builds the list from conditions.

Examples

Booleans and escaped values

disabled is written bare while busy, a false aria value stays the text false, and the quote in the title is escaped.

Templateexamples/attrs/1-boolean.pad
{set $busy = TRUE}
{set $help = 'Saves the "draft" now'}
<button {attrs disabled=$busy, title=$help, aria-expanded='false', data-step=2}>Save</button>
<button {attrs disabled=FALSE, title=$help}>Publish</button>
<pre>{attrs disabled=$busy, title=$help, aria-expanded='false', data-step=2}</pre>
Resultopen alone
disabled title="Saves the "draft" now" aria-expanded="false" data-step="2"

Attributes from an array

The keys of an array become attributes; the event handler, the false hidden and the javascript: address in the data are left out.

Templateexamples/attrs/2-array.pad
<a {attrs $link}>{$label}</a>
<input {attrs $field, required}>
<pre>{attrs $link}
{attrs $field, required}</pre>
PHPexamples/attrs/2-array.php
<?php

  $label = 'Documentation';

  $link = [ 'href'    => '?names',
            'title'   => 'All tags',
            'onclick' => 'steal()',
            'hidden'  => FALSE ];

  $field = [ 'type'        => 'email',
             'name'        => 'email',
             'placeholder' => 'you@example.com',
             'formaction'  => 'javascript:alert(1)' ];

?>
Resultopen alone
Documentation
href="?names" title="All tags"
type="email" name="email" placeholder="you@example.com" required

See also

  • {classes} Builds a class list from fixed names and names that are in only while their condition holds.
  • {input} A form field with its label that refills from what its form sent and shows the error validation found for it.
  • {echo} Evaluates an expression and prints the result - the general output tag, and the one to put in front of a pipe.