Kits

Templates and views

The kit's templates, the typed view each one executes with, and every core template function, with its signature.

A kit's templates are ordinary Go templates: html/template for HTML and text/template for text. Each one executes with a fixed, typed view, and lttr type-checks every template against its view when the kit loads. This page is the reference for both: what each template sees, and the functions the core supplies.

Know which template runs when

For every message, the renderer:

  1. executes each top-level component's html.tmpl with its kit.View, in document order, and joins the output (a nested component's template runs when its parent calls render);
  2. executes layout.html with a kit.LayoutView whose Body is that output;
  3. does the same for text: each text.tmpl, then layout.txt (see Plain text).

Inside a component, {{content .Content}} renders the component's Markdown through the defines of blocks.html (or the text blocks). styles.css is executed once per renderer, over the tokens and vars, and reaches the layout as .Styles.

TemplateKindDot
layout.htmlhtml/templatekit.LayoutView
layout.txttext/templatekit.TextLayoutView
styles.csstext/templatenil
blocks.html, blocks.txtdefineskit.BlockView; the action define gets a kit.ActionView
components/<name>/html.tmpl, text.tmplhtml/template, text/templatekit.View

Write the layout

layout.html is the whole document around the components. Plain's opens with the document head, then writes the head CSS:

plain/layout.html#L1-L13
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="x-apple-disable-message-reformatting">
<meta name="format-detection" content="telephone=no,date=no,address=no,email=no,url=no">
<meta name="color-scheme" content="light dark">
<meta name="supported-color-schemes" content="light dark">
<title>{{.Subject}}</title>
{{msoIf}}<noscript><xml><o:OfficeDocumentSettings><o:AllowPNG/><o:PixelsPerInch>96</o:PixelsPerInch></o:OfficeDocumentSettings></xml></noscript>{{msoEnd}}
<style>{{.Styles}}</style>
</head>

kit.LayoutView has Subject and Preheader (strings), Styles (the executed styles.css, as template.CSS), Body (the components' HTML, as template.HTML) and Doc. kit.TextLayoutView is the same without Styles, with Body as a string.

Plain writes its head by hand. A kit can call the core document function instead: it writes the doctype, <html> and the head metas through the Outlook settings block, with the color-scheme metas when the kit sets dark: true, and the layout writes the <title> and <style> after it. preheader writes the hidden preheader. See Email-safe HTML.

Read the component view

A component's html.tmpl and text.tmpl execute with a kit.View:

FieldTypeHolds
.NamestringThe component's name
.Attrskit.AttrsEvery attribute the schema declares, by name: its value, else its default, else the zero value of its type
.Bindkit.BindEvery binding the schema declares, by name, with its registered Go type. An optional binding not given is nil
.Contentkit.ContentThe compiled Markdown. Render it with {{content .Content}}
.Children[]kit.ChildViewThe nested components, in order
.Sectionkit.SectionThe resolved spacing and position
.Dockit.DocThe document-wide values

.Attrs and .Bind are maps, but the checker types each key from the schema: .Attrs.href in the button template is a template.URL, and .Bind.invoice in the invoice template is an Invoice whose fields are checked. An html attribute is the one whose type depends on the template: template.HTML in html.tmpl, and its plain-text alternative, a string, in text.tmpl (see Pass trusted HTML). Plain's section is one table row around its content:

plain/components/section/html.tmpl
<tr><td class="pl-px pl-text" align="left" style="padding:{{px .Section.Top}} {{token "size.side"}}px {{px .Section.Bottom}};{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:{{token "size.line"}}px;{{style "color" "color.text"}}">{{content .Content}}</td></tr>

A kit.ChildView has Name, Attrs, Bind, Content, Children, Section and Doc. Its Attrs and Bind (kit.ChildAttrs, kit.ChildBind) are typed by the child's schema, the one children.component names. Plain's columns reads its children inline, ranging over them and sizing each cell by their count:

plain/components/columns/html.tmpl
{{- $n := len .Children -}}
<tr><td class="pl-px" align="left" style="padding:{{px .Section.Top}} {{token "size.side"}}px {{px .Section.Bottom}};">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0"><tr>
{{- range $i, $c := .Children}}
{{- $pad := "0 12px 0 0"}}{{if eq $n 3}}{{$pad = "0 11px 0 0"}}{{end}}
{{- if and (eq $n 2) (eq $i 1)}}{{$pad = "0 0 0 12px"}}{{end}}
{{- if and (eq $n 3) (eq $i 1)}}{{$pad = "0 6px 0 5px"}}{{end}}
{{- if and (eq $n 3) (eq $i 2)}}{{$pad = "0 0 0 11px"}}{{end -}}
<td class="pl-col{{if $i}} pl-col-n{{end}}" width="{{if eq $n 3}}33%{{else}}50%{{end}}" align="left" valign="top" style="padding:{{$pad}};">{{content $c.Content}}</td>
{{- end}}</tr></table>
</td></tr>

kit.Section holds Top and Bottom (the resolved padding in px), Index (0-based among its siblings), First and Last, and Prev and Next (the neighbouring components' names, "" at the ends).

kit.Doc holds Subject, Preheader, Profile (Name and the formatted From), Recipient (Address, Name and a checked UnsubscribeURL) and Front (the kit's frontmatter keys). Plain's footer uses inline, var and the recipient's unsubscribe link:

plain/components/footer/html.tmpl
<tr><td class="pl-px" align="left" style="padding:{{px .Section.Top}} {{token "size.side"}}px {{px .Section.Bottom}};">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td class="pl-muted pl-rule" style="padding:24px 0 0;{{style "border-top" (printf "1px solid %s" (token "color.border"))}}{{style "font-family" "font.sans"}}font-size:{{token "size.small"}}px;line-height:{{token "size.small_line"}}px;{{style "color" "color.muted"}}">
{{- if .Attrs.note}}<p class="pl-muted" style="margin:0 0 12px;{{style "font-family" "font.sans"}}font-size:{{token "size.small"}}px;line-height:{{token "size.small_line"}}px;{{style "color" "color.muted"}}">{{inline .Attrs.note}}</p>{{end -}}
<p class="pl-muted" style="margin:0;{{style "font-family" "font.sans"}}font-size:{{token "size.small"}}px;line-height:{{token "size.small_line"}}px;{{style "color" "color.muted"}}">{{var "company"}} &middot; {{var "address"}}
{{- with .Doc.Recipient.UnsubscribeURL}} &middot;&nbsp;<a href="{{.}}" style="{{style "color" "color.accent"}}text-decoration:underline;">Unsubscribe</a>{{end -}}
</p>
</td></tr></table>
</td></tr>

Render nested components

Instead of reading a child's fields inline, a parent can hand the child to the core render function, which executes the child's own html.tmpl (or text.tmpl, in a text template) with a kit.View of the child, and returns its output. Plain and Quiet nest one level and read their children inline; this example adds three components to a copy of Plain:

components/steps/html.tmpl
<tr><td class="pl-px" align="left" style="padding:{{px .Section.Top}} {{token "size.side"}}px {{px .Section.Bottom}};">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0">
{{- range .Children}}{{render .}}{{end -}}
</table>
</td></tr>

Here steps takes :::step children, and step takes at most one ::::tip of its own (children: { component: tip, max: 1 }). The step's template sees its tip as .Children and renders it the same way:

components/step/html.tmpl
<tr><td class="pl-text" style="padding:0 0 {{token "size.block"}}px;{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:{{token "size.line"}}px;{{style "color" "color.text"}}">
<p style="margin:0 0 4px;font-weight:600;">{{.Attrs.title}}</p>
{{content .Content}}
{{- range .Children}}{{render .}}{{end -}}
</td></tr>
  • Any depth. A child may declare children too, and render works at every level, up to the parser's 32.
  • One view per template. A component's templates always run with a kit.View, whether it is top-level or nested, so each is checked once, against its own schema. Inside a child's template, .Children are its own children.
  • Text. In text.tmpl, render returns the child's text form with trailing newlines trimmed, or "" when the child has no text.tmpl.
  • Spacing. .Section is resolved among each set of siblings with the child's own schema.
  • Errors from a child's template are reported at the child: its Markdown position and name, then its kit file. An error from deeper down keeps the deepest position.

Reading a child's fields inline and calling render mix freely: {{range .Children}}<h2>{{.Attrs.title}}</h2>{{render .}}{{end}}.

Grandchildren are reached through render, not inline. In any template, ChildAttrs and ChildBind are typed by that component's direct children. So a grandparent that reads its grandchildren's fields inline, {{range .Children}}{{range .Children}}{{.Attrs.title}}{{end}}{{end}}, is checked against the child's schema: a key only the grandchild has is a load error, and a key the child has passes the check and can fail when it renders. With title on step but not on tip:

Terminal
$ lttr kit check ./emailkit
emailkit/samples/steps.md:8:1: steps: emailkit/components/steps/html.tmpl:3:49: map has no entry for key "title"
lttr kit check: ./emailkit: 1 problem

Render Markdown blocks

blocks.html defines one template per block kind. {{content .Content}} executes each top-level block through the define named by its kind and joins the output verbatim. Every kit defines paragraph, heading, list, actions, action, table and inline, plus image when a component allows images. Plain's action row and action:

plain/blocks.html#L33-L41
{{define "actions"}}<table role="presentation" cellpadding="0" cellspacing="0" border="0"><tr>
{{- range .Actions}}<td valign="middle" style="padding:0 16px {{token "size.block"}}px 0;">{{template "action" .}}</td>{{end -}}
</tr></table>{{end}}

{{define "action"}}
{{- if .Primary}}<table role="presentation" cellpadding="0" cellspacing="0" border="0"><tr><td class="pl-btn" bgcolor="{{token "color.button"}}" style="{{style "background-color" "color.button"}}mso-padding-alt:10px 20px;"><a class="pl-btn-a" href="{{.Href}}" style="display:inline-block;padding:10px 20px;{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:20px;font-weight:600;{{style "color" "color.on_button"}}text-decoration:none;">{{.Label}}</a></td></tr></table>
{{- else}}<a class="pl-link" href="{{.Href}}" style="{{style "font-family" "font.sans"}}font-size:{{token "size.text"}}px;line-height:20px;font-weight:600;{{style "color" "color.accent"}}text-decoration:none;">{{.Label}}{{if .Arrow}}&nbsp;&rarr;{{end}}</a>
{{- end}}
{{- end}}

Each define executes with a kit.BlockView:

FieldHolds
Kindparagraph, heading, list, actions, table, image, or inline for the inline define
VariantThe variant passed to content or inline, "" for none
Index, First, Last, PrevThe block's position among the content's top-level blocks, and the previous block's kind
LevelA heading's level, 1 to 6
Ordered, NumberAn ordered list; a list item's 1-based number
HTMLThe rendered inline HTML of a paragraph, heading, list item or inline value
Text, LinesThe plain text, soft breaks as spaces (Text) or kept as "\n" (Lines). For a table, Text is the core text layout
InlineThe inline nodes ([]kit.InlineView), for kits that render inlines themselves
ItemsA list's items, each a BlockView
ActionsAn action row's items, each a kit.ActionView
Header, Rows, AlignA table's header cells, body rows (kit.CellView) and column alignments ("", left, center, right)
Src, Alt, TitleAn image's checked source, alt text and title
  • kit.ActionView: Label, Href (checked), Primary (a :button, not a :link) and Arrow (a :link that asks for a trailing arrow).
  • kit.CellView: HTML, Text, Lines, Inline and Strong (the whole cell is bold).
  • kit.InlineView: Kind (text, code, strong, em, strike, link, span or break), Text, Href, Title, Classes, Hard and Children.

HTML already carries the kit's inline and spans styles, so most kits print {{.HTML}} and never touch Inline.

Pass a variant

content and inline take one optional variant string: {{content .Content "compact"}}. It is copied into BlockView.Variant of every block and list item, so one blocks.html can render the same Markdown two ways ({{if eq .Variant "compact"}}…). A second variant is a render error.

Use the core functions

The core supplies these functions to every kit template. Their signatures are exact: the checker types every call against them when the kit loads. The names cannot be reused by kit.Funcs.

In every template

FunctionSignatureWhat it does
tokentoken(name string) stringA token's value: the value, or a colour's light value
darkdark(name string) stringA colour token's dark value
varvar(name string) stringA kit var: the renderer's Config.Vars value, else the kit default
pxpx(n int) stringn as "Npx", or "0" for zero
upperupper(s string) strings in capitals
failfail(msg string) (string, error)Always an error with msg, for rules a schema cannot express. Not in styles.css

index is the text/template builtin with one change at render time: a missing map key is an error (index of missing key "gold") instead of an empty value.

In HTML templates

FunctionSignatureWhat it does
contentcontent(c kit.Content, variant ...string) (template.HTML, error)Renders content through the blocks.html defines
renderrender(v kit.ChildView) (template.HTML, error)Executes a nested component's own html.tmpl with a kit.View of it. See Render nested components
inlineinline(v kit.Inline, variant ...string) (template.HTML, error)Renders an inline attribute or frontmatter value through the inline define. A nil value renders nothing
stylestyle(kv ...string) (template.CSS, error)Builds prop:value; pairs. A value that names a token becomes the token's value. Empty values are left out. An odd count, a property outside [a-z-]+, or a value with ;, {, }, <, >, ", \, an unpaired ', a comment, url( or expression( is an error
urlurl(s string) (template.URL, error)Checks a URL built in the template: https, http, mailto or tel, no whitespace. Relative URLs only in previews
msoOpenmsoOpen(width int) template.HTMLOpens an Outlook-only centred table width px wide
msoClosemsoClose() template.HTMLCloses it
msoIf, msoEndmsoIf() template.HTML, msoEnd() template.HTMLWrap markup only Outlook renders
notMsoOpen, notMsoClosenotMsoOpen() template.HTML, notMsoClose() template.HTMLWrap markup every client but Outlook renders
buttonbutton(href template.URL, label, fill, text string, width, height int) template.HTMLA bulletproof pill button: VML for Outlook, a link for everyone else. fill and text must be a #hex colour or a keyword
preheaderpreheader(s string) template.HTMLThe hidden preheader <div>
documentdocument() template.HTMLThe doctype, <html> and the head metas, through the Outlook settings block

In text templates

FunctionSignatureWhat it does
contentcontent(c kit.Content, variant ...string) (string, error)Renders content through the text block defines
renderrender(v kit.ChildView) (string, error)Executes a nested component's own text.tmpl, trailing newlines trimmed; "" when it has none
inlineinline(v kit.Inline, variant ...string) (string, error)Renders an inline value as text
wrapwrap(s string) stringWraps at 70 characters
leadersleaders(rows ...kit.Leader) stringDot-leader rows, values in one column
leaderleader(label, value string, sub ...string) kit.LeaderOne dot-leader row, with at most one sub-value
columnscolumns(header []string, rows [][]string, right []bool) stringPadded columns
cells, rows, boolscells(v ...string) []string, rows(r ...[]string) [][]string, bools(b ...bool) []boolBuild the arguments of columns

Plain text shows the text functions at work. styles.css gets token, dark, var, px and upper.

Know how errors surface

A function error, a missing map key or a nil pointer while rendering is a render error carrying two positions: the component in the Markdown template, then the kit file. fail reports its message alone. One line from each of three lttr kit check ./emailkit runs:

Terminal
emailkit/samples/welcome.md:8:1: section: emailkit/components/section/html.tmpl:1:128: error calling style: style: odd number of arguments (1)
emailkit/samples/welcome.md:16:1: button: emailkit/components/button/html.tmpl:2:267: error calling url: url "javascript:alert(1)": scheme "javascript" not allowed
emailkit/samples/welcome.md:19:1: image: width must be at least 1

The first two come from a copy of Plain broken on purpose. The third comes from a sample with width="0", which Plain's own image template refuses with fail:

plain/components/image/html.tmpl#L1-L3
{{- /* Outlook sizes images from the width attribute, so it is capped at the
content width (size.content). */ -}}
{{- $w := .Attrs.width}}{{if lt $w 1}}{{fail "width must be at least 1"}}{{end}}{{if gt $w 496}}{{$w = 496}}{{end -}}

Write the head CSS

styles.css is a text/template over the tokens and vars, written into the layout's <style> as .Styles. It holds what inline styles cannot: resets, the phone breakpoint and the dark palette. Plain's phone rules use size tokens:

plain/styles.css#L7-L16
@media only screen and (max-width:599px){
.pl-outer{padding:0!important}
.pl-surface{border:0!important}
.pl-px{padding-left:{{token "size.side_mobile"}}px!important;padding-right:{{token "size.side_mobile"}}px!important}
.pl-h1{font-size:{{token "size.h1_mobile"}}px!important;line-height:{{token "size.h1_mobile_line"}}px!important}
.pl-col{display:block!important;width:100%!important;padding:0!important}
.pl-col-n{padding-top:8px!important}
.pl-btn-t{width:100%!important}
.pl-btn-a{display:block!important;text-align:center!important}
}

Because it is executed as text and its output is trusted as CSS, token and var values may not contain characters that could end the <style> element or a rule (see kit.yaml).

Next steps

Copyright © 2026