Kits

Template checking

What kit.Load checks in every kit template before anything renders, the errors it reports, and the mistakes that only surface at render time.

kit.Load does not stop at parsing. It type-checks every template in the kit against the view it executes with, so a typo in a field, an attribute, a token or a function call fails when the kit loads, with its position in the kit file. It never waits for the first email that happens to reach that line.

Catch a typo at load

The tutorial's invoice template reads the bound Invoice through .Bind.invoice:

tutorial/kit/components/invoice/html.tmpl
{{- /* The title and number, one row per line (name, quantity, amount),
then the total. .Bind.invoice is an Invoice, so every field below is
checked against the Go type when the kit loads. */ -}}
<tr><td class="pl-px" align="left" style="padding:{{px .Section.Top}} {{token "size.side"}}px {{px .Section.Bottom}};">
<p class="pl-text" style="margin:0 0 8px;{{style "font-family" "font.sans"}}font-size:{{token "size.h2"}}px;line-height:{{token "size.h2_line"}}px;font-weight:600;{{style "color" "color.text"}}">{{.Attrs.title}} {{.Bind.invoice.Number}}</p>
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse:collapse;">
{{- range .Bind.invoice.Lines}}
<tr><td class="pl-text pl-rule" align="left" style="padding:10px 0;{{style "border-bottom" (printf "1px solid %s" (token "color.border"))}}{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:{{token "size.line"}}px;{{style "color" "color.text"}}">{{.Name}}</td>
<td class="pl-muted pl-rule" align="right" style="padding:10px 0 10px 12px;{{style "border-bottom" (printf "1px solid %s" (token "color.border"))}}{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:{{token "size.line"}}px;{{style "color" "color.muted"}}white-space:nowrap;">&times;&nbsp;{{.Qty}}</td>
<td class="pl-text pl-rule" align="right" style="padding:10px 0 10px 12px;{{style "border-bottom" (printf "1px solid %s" (token "color.border"))}}{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:{{token "size.line"}}px;{{style "color" "color.text"}}white-space:nowrap;">{{.Amount}}</td></tr>
{{- end}}
<tr><td class="pl-text" colspan="2" align="left" style="padding:10px 0 0;{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:{{token "size.line"}}px;font-weight:600;{{style "color" "color.text"}}">Total</td>
<td class="pl-text" align="right" style="padding:10px 0 0 12px;{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:{{token "size.line"}}px;font-weight:600;{{style "color" "color.text"}}white-space:nowrap;">{{.Bind.invoice.Total}}</td></tr>
</table>
</td></tr>

Misspell .Bind.invoice.Total on line 13 as .Bind.invoice.Totl, and the kit does not load:

kit.Load error
components/invoice/html.tmpl:13:243: Invoice has no field or method "Totl"

The checker knows .Bind.invoice is an Invoice because the schema binds invoice: { type: Invoice } and Go registered Invoice under that name. The position is the line and the byte column of the typo. The tutorial pins this exact error in a test, so the docs and the code fail together if either changes:

tutorial/tutorial_test.go#L55-L85
// TestTypo: a misspelt field on a bound type fails kit.Load, with the
// position of the typo in the kit file.
func TestTypo(t *testing.T) {
    overlay := fstest.MapFS{}
    err := fs.WalkDir(Files(), ".", func(path string, d fs.DirEntry, err error) error {
        if err != nil || d.IsDir() {
            return err
        }
        b, err := fs.ReadFile(Files(), path)
        if err != nil {
            return err
        }
        overlay[path] = &fstest.MapFile{Data: b}
        return nil
    })
    if err != nil {
        t.Fatal(err)
    }
    const tmpl = "components/invoice/html.tmpl"
    src := string(overlay[tmpl].Data)
    if !strings.Contains(src, ".Bind.invoice.Total") {
        t.Fatalf("%s no longer shows .Bind.invoice.Total", tmpl)
    }
    overlay[tmpl].Data = []byte(strings.Replace(src, ".Bind.invoice.Total", ".Bind.invoice.Totl", 1))

    _, err = load(overlay)
    const want = `components/invoice/html.tmpl:13:243: Invoice has no field or method "Totl"`
    if err == nil || err.Error() != want {
        t.Errorf("load error = %v\nwant %s", err, want)
    }
}

Know what Load checks

kit.Load runs every check and reports every problem it finds, not just the first:

  1. YAML. Every key of kit.yaml and each component.yaml is known, every value has the right type, names are valid, token values are CSS-safe, and attribute rules agree with each other.
  2. References. default, children, must_follow and spacing.after name real components or classes. Aliases are unique. Every types entry is registered, and every binding names a listed type.
  3. Files and defines. layout.html, layout.txt, styles.css, blocks.html and each component's html.tmpl exist. blocks.html defines every block the kit needs.
  4. Parsing. Every template parses, with every function it calls known.
  5. Escaping. html/template's contextual escaper runs over every HTML template, so an action in a context it cannot escape (an unclosed attribute, say) fails now, not at the first render.
  6. Types. Every template is walked with the static type of its dot, as below.

The type check follows the dot through with, range (over slices, arrays, maps and integers), if, variables and {{template "name" .}} calls, and checks:

  • Field and method chains on the views and on bound Go types, including methods such as .Amount.String.
  • Schema keys: .Attrs.x, .Bind.x and .Doc.Front.x must be declared by the component's schema or kit.yaml, and each is typed from it. A child's .Attrs and .Bind are typed by the child's schema. An html attribute is template.HTML in html.tmpl and string in text.tmpl. Literal keys given to index are checked too. An unknown key gets the closest declared one as a suggestion.
  • Function calls: the number and types of the arguments of every core function and every kit.Funcs function.
  • Literal names: the string argument of token, dark and var must name a token or var, with a suggestion when one is close. token suggests only tokens with a light value, and dark only tokens with a dark value. A literal value given to style that reads as a token name (dotted lower-case segments, such as color.txt, whose first segment is the group of one of the kit's tokens) must be a token with a light value too. CSS values such as 0.5em, rgb(0 0 0 / .5) or a font stack never read as one.
  • Ranges and templates: range over something that cannot be ranged, and calls to templates that do not exist.

Read the errors

Every error has the form path:line:col: message, with a path relative to the kit root and a 1-based byte column. Parse errors carry a line only. kit.Load sorts them by file and position, drops duplicates, stops at 50 per file (then N more errors) and returns them joined with errors.Join, each a *kit.Error with Path, Line, Col and Msg.

These come from lttr kit check, each on a copy of Plain with one mistake. The CLI puts the kit directory in front of each path:

MistakeError
A misspelt tokenemailkit/components/section/html.tmpl:1:179: token: unknown token "size.txt" (did you mean size.text?)
A misspelt token in styleemailkit/components/section/html.tmpl:1:247: style: unknown token "color.txt" (did you mean color.text?)
A misspelt varemailkit/components/footer/html.tmpl:4:188: var: unknown var "compnay" (did you mean company?)
An attribute the schema does not declareemailkit/components/button/html.tmpl:2:496: unknown key "lable" in Attrs (did you mean label?)
A string where px wants an intemailkit/components/button/html.tmpl:1:56: px: argument 1 is "12", want int
range over a text attributeemailkit/components/button/html.tmpl:2:496: range over string
A function that does not existemailkit/components/section/text.tmpl:1: function "money" not defined
A block define removed while a component allows imagesemailkit/blocks.html: missing define "image"
An attribute left open at the end of a defineemailkit/blocks.html: ends in a non-text context: {stateAttr delimDoubleQuote urlPartNone jsCtxRegexp [] attrNone elementNone <nil>}

The last one is html/template's own escaping error, raised at load: the inline define had become <b title="{{.HTML}}.

Know the limits

The checker is conservative. It reports only what it can prove, so some mistakes pass it:

  • Interfaces are untyped. A value whose static type is an interface, any included, stops the check along that chain without an error. .Content is an interface, but it only goes to content. Bound Go types are concrete, so they are checked in full.
  • A variable reassigned to a value of a different type becomes untyped.
  • Computed names passed to token, dark or var are checked when they render, not at load.
  • Grandchildren read inline are checked against the child's schema, not the grandchild's: in any template, ChildAttrs and ChildBind are typed by that component's direct children. A key the child also declares passes the check and can fail at render. Reach grandchildren through render instead. See Render nested components.
  • Three cases pass the check and fail at render: a method on *T reached through a value that is not addressable (a map element, an index result), a method with no results, and range $i, $j over an int or an iter.Seq.

Values are never known at load either. An index out of range, a nil pointer inside a bound value, a URL that fails its check, an unsafe style value, or a fail all surface when a template renders. They carry the Markdown position and the kit position, so they are still easy to find:

Terminal
$ lttr kit check ./emailkit
emailkit/samples/welcome.md:16:1: button: emailkit/components/button/text.tmpl:1:3: error calling index: index out of range: 99
lttr kit check: ./emailkit: 1 problem

lttr kit check, and your own tests, render every sample, which is how these are caught before a real send. See Go types and funcs for a kit the CLI cannot load.

Next steps

Copyright © 2026