What is a kit
lttr's engine has no design of its own. Everything a recipient sees comes from a kit: the colours and fonts, the document shell, the components a Markdown template may use, and how each one renders as HTML and as plain text. You own the kit. It is a directory of YAML and Go templates, loaded as an fs.FS, usually an embed.FS in a Go package of yours.
This is Plain, the starter kit, rendering its welcome sample. Everything in it, from the canvas colour to the footer, is in Plain's files:
Welcome to Example, Ada
From: Example <hello@example.com>
Lay out the files
Every kit has the same layout:
kit.yaml name, default component, dark, tokens, vars, spans, inline styles, limits, types, frontmatter
layout.html, layout.txt the document shells
styles.css head CSS: a text/template over the tokens
blocks.html, blocks.txt how Markdown blocks render (blocks.txt is optional)
components/<name>/component.yaml the component's schema
components/<name>/html.tmpl its HTML
components/<name>/text.tmpl its text form (optional: the component has none)
assets/ optional: logos and images
samples/*.md, samples/samples.yaml optional: sample templates and their data
Each file has one job, and each template runs with one fixed view:
| File | Written in | Runs with |
|---|---|---|
kit.yaml | YAML | See kit.yaml |
layout.html | html/template | kit.LayoutView: the subject, preheader, styles and the components' HTML |
layout.txt | text/template | kit.TextLayoutView: the subject, preheader and the components' text |
styles.css | text/template | No data: it is executed once per renderer, over the tokens and vars |
blocks.html | html/template defines | kit.BlockView, one per Markdown block (action gets a kit.ActionView) |
blocks.txt | text/template defines | The same views. Defines it leaves out use lttr's core text forms |
component.yaml | YAML | See Component schemas |
html.tmpl, text.tmpl | html/template, text/template | kit.View: the component's attributes, bindings, content, children, spacing and the document |
A component's directory name is its name. It must match [a-z][a-z0-9_-]*, and a name: key in component.yaml, when present, must match the directory. A Markdown template uses the component as ::name{…}. Loose Markdown outside any component goes to the kit's default component. Templates and views covers the views field by field.
Start from Plain
lttr kit init copies Plain into a directory and renames it after the directory:
$ lttr kit init ./emailkit
wrote 29 files to ./emailkit; check it with: lttr kit check ./emailkit
$ lttr kit check ./emailkit
kit emailkit: 2 samples rendered, no problems
Without an installed lttr, run it as go run github.com/sulv-io/lttr/cmd/lttr@latest kit init ./emailkit.
- The name is the directory's base name. It must be lower-case letters, digits and
-. Otherwise pass-name, before the directory:lttr kit init -name tutorial examples/tutorial/kit. - What changes:
name:inkit.yaml, and its first comment line, which becomes# emailkit: an lttr kit, started from the Plain kit by `lttr kit init`.Every other file is Plain's, byte for byte. - What
kit checkdoes: it loads the kit, renders everysamples/*.mdwith its data fromsamples/samples.yaml, lints the HTML and exits 1 on any problem. See Email-safe HTML for the lint and CLI for the flags. - Samples are your test cases.
samples/samples.yamlmaps each sample file name to its literal merge data: strings, numbers, booleans, lists and maps, with no Go types.kit checkrenders only what the samples use, so a new component must appear in somesamples/*.mdbeforekit checkrenders and lints it.
Plain's data for its welcome sample:
# Literal merge data for each sample, keyed by file name. `lttr kit check`
# renders every sample with these values.
welcome.md:
first_name: Ada
workspace: Northwind Studio
trial_ends: 9 October
setup_url: https://example.com/setup
cover_url: https://example.com/assets/cover.png
From here, change anything. Plain's kit.yaml header says where to start:
# Plain: a small, neutral starter kit for lttr.
#
# One left-aligned column on a light canvas, a system sans-serif stack,
# square buttons and light/dark colours. Copy it with `lttr kit init` and
# change anything: tokens here, markup in layout.html, blocks.html and
# components/*/html.tmpl.
name: plain
version: 1
default: section
dark: true
The Build your own kit guide takes this copy to a kit with its own colours, font and an invoice component that binds a Go type.
Load a kit from Go
Embed the kit directory in a Go package and load it once. This is the tutorial kit's package, from the guide:
// files holds the kit directory, kit/.
//
//go:embed kit
var files embed.FS
// Files returns the kit's files, rooted at kit/.
func Files() fs.FS {
sub, err := fs.Sub(files, "kit")
if err != nil {
panic(err) // "kit" is a valid path, so Sub cannot fail
}
return sub
}
// LoadKit loads the tutorial kit from fsys (a copy of kit/ on disk, say)
// with the Go types its components bind. A kit with `types` in kit.yaml
// loads only with every listed type registered. Kit() is
// LoadKit(Files()) cached.
func LoadKit(fsys fs.FS) (*kit.Kit, error) {
return kit.Load(fsys, kit.Types(kit.TypeOf[Invoice]("Invoice")))
}
// load is LoadKit, kept for callers within the package.
func load(fsys fs.FS) (*kit.Kit, error) { return LoadKit(fsys) }
// loadOnce loads the kit once, so every template and renderer shares the
// same immutable *kit.Kit.
var loadOnce = sync.OnceValues(func() (*kit.Kit, error) { return LoadKit(Files()) })
// Kit returns the loaded tutorial kit. It is safe for concurrent use and
// returns the same *kit.Kit on every call.
func Kit() (*kit.Kit, error) { return loadOnce() }
// MustKit is Kit that panics on error; the embedded kit is tested, so an
// error means a broken build.
func MustKit() *kit.Kit {
k, err := Kit()
if err != nil {
panic(err)
}
return k
}
fs.Submatters:kit.Loadexpectskit.yamlat the root of thefs.FS.kit.Loaddecodes every file, checks every reference between them, runshtml/template's escaper over the HTML templates and type-checks every template against its view. It returns the kit, or every problem it found, joined, each at its position in a kit file. See Template checking.- Options pass what only Go can supply:
kit.Typesregisters the Go types components bind, andkit.Funcsadds template functions. See Go types and funcs. - A loaded
*kit.Kitis immutable and safe for concurrent use. Compile every template against it (lttr.Compile[T](k, name, src)) and build every renderer on it (lttr.Config{Kit: k}). A template renders only with a renderer built on the same*kit.Kitit was compiled against. A secondkit.Loadof the same files is a different kit, so load once and share it.
The shipped kits follow the same pattern: plain.Kit(), plain.MustKit() and plain.Files() in github.com/sulv-io/lttr/kits/plain. plain.Set(cfg) returns Plain's samples as an lttr.SampleSet, built from samples/ by lttr.KitSamples, the same set lttr kit check renders. See Goldens and testing.
Meet the two shipped kits
| Plain | Quiet | |
|---|---|---|
| Package | github.com/sulv-io/lttr/kits/plain | github.com/sulv-io/lttr/kits/quiet |
| Design | One left-aligned column, a system sans-serif stack, square buttons, light and dark colours | A calm, centred design with a serif display face |
| Components | section (the default), button, image, columns (of column), footer | 24, from statement and receipt to stories and signoff; letter is the default |
| Go types and functions | None: everything it does is in its files | Registers Go types (Money, Receipt, Product, …) and template functions |
| Use it for | The base of your own kit, via lttr kit init | A complete reference: the nine Loam emails in examples/quiet are the engine's conformance suite |
The gallery renders every sample of both kits. The Quiet kit guide walks through Quiet.
Next steps
- kit.yaml: tokens, vars and every other key.
- Component schemas: what a component accepts.
- Templates and views: what a template sees, and every core function.
Compile errors
What lttr.Compile reports, how to read it in Go and from the CLI, the warnings, and the common errors with their fixes.
kit.yaml
Every key of kit.yaml — name, default component, dark mode, tokens, vars, spans, inline styles, limits, Go types and frontmatter — and how token names resolve.