Skip to content

The html template

lib/html.js is the whole rendering library, about 40 lines. It builds HTML strings safely:

render(root, html`<a href="#/album/${album.id}">${album.title}</a>`);
  • Every ${value} is escaped, so a song titled <script> shows as text.
  • Nesting works. A value that is itself an html`` result, or an array of them, is inserted as-is. That's how lists are drawn: ${items.map((i) => html`<li>${i}</li>`)}.
  • raw(markup) skips escaping. Use it only for markup the code itself wrote.
  • Rendering replaces innerHTML wholesale. There's no diffing. Components that update often (the player bar) draw once and then change individual elements.

Watch for this

null, undefined and false render as nothing. That's convenient for ${cond ? html`…` : ''} but breaks ARIA attributes: aria-expanded="${open}" becomes aria-expanded="" when open is false. Write aria-expanded="${String(open)}".