HTML templates

An html:NAME block is a function from data to a string. It is parsed into nodes when the file is loaded, and html("NAME") renders it; html("NAME", {"k": v}) binds the names its holes read, so a template can be called from anywhere rather than only where its data happens to be in scope.

This page is about the rules that catch people. Routes and serving shows where a template is used.

The syntax

Indentation is the nesting. A line is an element, | starts literal text, doctype html writes the doctype, if/else branch and for name in list loops over the lines indented under them.

card.md
---
midad: "0.1"
module: card
---

```html:card
doctype html
html
  body
    h1 {{ title }}
    | plain text
    for n in items
      li {{ n }}
```

```exec
print(html("card", {"title": "T", "items": ["x"]}));
```
output
<!doctype html><html><body><h1>T</h1>plain text<li>x</li></body></html>

Nested elements come out on one line and nothing is indented for you: the bytes are the ones you wrote. There are no partials, no filters and no whitespace control, so a template that needs a newline in its output writes one.

Escaped and raw holes

{{ expr }} inserts a value with HTML escaping on. {{! expr }} inserts it raw, which is what you want for a fragment another template rendered and what you must not use for anything a user typed.

escape.md
---
midad: "0.1"
module: escape
---

```html:row
p {{ body }}
p {{! body }}
```

```exec
const body = "<b>x</b>";
print(html("row"));
```
output
<p>&lt;b&gt;x&lt;/b&gt;</p><p><b>x</b></p>

html("row") with no map renders the holes it can see from the surrounding scope, which is why body needs no binding here.

A template has no typed parameters

The second argument is a map of values, so every bound hole is typed any inside the template. A hole that expects a list and is handed a number fails at render time, not at check time, and the checker cannot help: the template's parameters are not declared anywhere.

What the checker does catch is a hole no scope and no map can fill, when the map is written out at the call. Its literal keys are bound for the hole check, so a misspelled key is E0241:

check-3.md
---
midad: "0.1"
module: hole
---

```html:row
p {{ name }}
```

```exec
print(html("row", {"nme": "a"}));
```
midad check
× E0241: unknown variable `name`

Put the same map in a variable and that check is gone, because the keys are no longer known at check time. midad check passes, and the hole fails when the template renders:

error-4.md
---
midad: "0.1"
module: hole2
---

```html:row
p {{ name }}
```

```exec
const m = {"nme": "a"};
print(html("row", m));
```
midad run (exit 1)
error-4.md:12:7
  × R0001: unknown variable `name`
    ╭─[error-4.md:12:7]
 11 │ const m = {"nme": "a"};
 12 │ print(html("row", m));
    ·       ───────┬──────
    ·              ╰── unknown variable `name`
 13 │ ```
    ╰────

So the map form buys a template that can be called from anywhere, at the price of the checker seeing its holes only when the map is written at the call.

A native build binds a hole by variable name

The interpreter reads a hole's value out of the map you pass, so html("t", {"name": other}) renders other. A native build reads it from the variable named after the hole, and refuses anything else by name rather than render the wrong value:

`html` mapping `name` to something other than the variable `name`: a native
build reads a template's holes by name

The map form itself is refused there too, since a map held in a variable has no keys the compiler can see:

`html` with a map held in a variable: a native build binds a template's holes
by name, so write the map out at the call as {"name": name}

On a native build, write the map out at the call and map each key to the variable of that name, or call html("t") and let the holes read the scope.

Branches and loops on every backend

All three backends render a template that branches or loops, and a test renders the same template through each and compares the bytes. One native limit follows from the native subset having no dynamic values: a loop variable is a string, number or float, so a loop over a list of lists is refused by name on a native build.

On this page