Skip to content

The Attribute Rendering Process

Carl Smith edited this page May 30, 2018 · 5 revisions

Rendering attributes does not affect the attributes or the container they are in (rendering never mutates anything). However, the names and values are processed automatically as they are rendered.

Null Attributes

Attributes that currently have None values are not rendered at all.

Serializing Values

Attribute values are passed through str and the result is rendered, so the value can be a number for example.

Escaping Values

Attribute values are always wrapped in doublequotes, and are escaped automatically. The escape function escapes doublequotes, open angle brackets and ampersands.

Note: HTME does not escape anything besides attribute values:

>>> DIV({"name": "spam & eggs"}, "foo & bar")
<div name="spam &amp; eggs">foo & bar</div>

Boolean Attributes

Attributes that currently have empty string values, are rendered as boolean attributes:

>>> DIV({"contenteditable": ""})
<div contenteditable></div>

Data Attributes

Attribute names that begin with a dollar, are shorthand data attributes. The dollar is expanded in the rendered name to the string data followed by a hyphen:

>>> DIV({"$visitors": "1000000"})
<div data-visitors="1000000"></div>

Determinism

Attribute names are rendered with the names sorted (asciibetically) to make rendering deterministic. The element attributes are rendered first, with the data attributes after them:

>>> DIV({"id": "selected", "$mass": 145.34, "class": "product", "$price": 50})
<div class="product" id="selected" data-price="50" data-mass="145.34"></div>

Sequential Values

You can provide a list or tuple (or any subclass) as an attribute value, in which case, each item in the sequence is converted according to the same rules for single values, and the results are joined with spaces (skipping any that are None) to form a string:

>>> SVG({"viewbox": (1, 1, 100, 100)})
<svg viewbox="1 1 100 100"></svg>

Clone this wiki locally