Templates and views
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:
- executes each top-level component's
html.tmplwith itskit.View, in document order, and joins the output (a nested component's template runs when its parent callsrender); - executes
layout.htmlwith akit.LayoutViewwhoseBodyis that output; - does the same for text: each
text.tmpl, thenlayout.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.
| Template | Kind | Dot |
|---|---|---|
layout.html | html/template | kit.LayoutView |
layout.txt | text/template | kit.TextLayoutView |
styles.css | text/template | nil |
blocks.html, blocks.txt | defines | kit.BlockView; the action define gets a kit.ActionView |
components/<name>/html.tmpl, text.tmpl | html/template, text/template | kit.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:
<!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:
| Field | Type | Holds |
|---|---|---|
.Name | string | The component's name |
.Attrs | kit.Attrs | Every attribute the schema declares, by name: its value, else its default, else the zero value of its type |
.Bind | kit.Bind | Every binding the schema declares, by name, with its registered Go type. An optional binding not given is nil |
.Content | kit.Content | The compiled Markdown. Render it with {{content .Content}} |
.Children | []kit.ChildView | The nested components, in order |
.Section | kit.Section | The resolved spacing and position |
.Doc | kit.Doc | The 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:
<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:
{{- $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:
<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"}} · {{var "address"}}
{{- with .Doc.Recipient.UnsubscribeURL}} · <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:
<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:
<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
childrentoo, andrenderworks 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,.Childrenare its own children. - Text. In
text.tmpl,renderreturns the child's text form with trailing newlines trimmed, or""when the child has notext.tmpl. - Spacing.
.Sectionis 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:
$ 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:
{{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}} →{{end}}</a>
{{- end}}
{{- end}}
Each define executes with a kit.BlockView:
| Field | Holds |
|---|---|
Kind | paragraph, heading, list, actions, table, image, or inline for the inline define |
Variant | The variant passed to content or inline, "" for none |
Index, First, Last, Prev | The block's position among the content's top-level blocks, and the previous block's kind |
Level | A heading's level, 1 to 6 |
Ordered, Number | An ordered list; a list item's 1-based number |
HTML | The rendered inline HTML of a paragraph, heading, list item or inline value |
Text, Lines | The plain text, soft breaks as spaces (Text) or kept as "\n" (Lines). For a table, Text is the core text layout |
Inline | The inline nodes ([]kit.InlineView), for kits that render inlines themselves |
Items | A list's items, each a BlockView |
Actions | An action row's items, each a kit.ActionView |
Header, Rows, Align | A table's header cells, body rows (kit.CellView) and column alignments ("", left, center, right) |
Src, Alt, Title | An image's checked source, alt text and title |
kit.ActionView:Label,Href(checked),Primary(a:button, not a:link) andArrow(a:linkthat asks for a trailing arrow).kit.CellView:HTML,Text,Lines,InlineandStrong(the whole cell is bold).kit.InlineView:Kind(text,code,strong,em,strike,link,spanorbreak),Text,Href,Title,Classes,HardandChildren.
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
| Function | Signature | What it does |
|---|---|---|
token | token(name string) string | A token's value: the value, or a colour's light value |
dark | dark(name string) string | A colour token's dark value |
var | var(name string) string | A kit var: the renderer's Config.Vars value, else the kit default |
px | px(n int) string | n as "Npx", or "0" for zero |
upper | upper(s string) string | s in capitals |
fail | fail(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
| Function | Signature | What it does |
|---|---|---|
content | content(c kit.Content, variant ...string) (template.HTML, error) | Renders content through the blocks.html defines |
render | render(v kit.ChildView) (template.HTML, error) | Executes a nested component's own html.tmpl with a kit.View of it. See Render nested components |
inline | inline(v kit.Inline, variant ...string) (template.HTML, error) | Renders an inline attribute or frontmatter value through the inline define. A nil value renders nothing |
style | style(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 |
url | url(s string) (template.URL, error) | Checks a URL built in the template: https, http, mailto or tel, no whitespace. Relative URLs only in previews |
msoOpen | msoOpen(width int) template.HTML | Opens an Outlook-only centred table width px wide |
msoClose | msoClose() template.HTML | Closes it |
msoIf, msoEnd | msoIf() template.HTML, msoEnd() template.HTML | Wrap markup only Outlook renders |
notMsoOpen, notMsoClose | notMsoOpen() template.HTML, notMsoClose() template.HTML | Wrap markup every client but Outlook renders |
button | button(href template.URL, label, fill, text string, width, height int) template.HTML | A bulletproof pill button: VML for Outlook, a link for everyone else. fill and text must be a #hex colour or a keyword |
preheader | preheader(s string) template.HTML | The hidden preheader <div> |
document | document() template.HTML | The doctype, <html> and the head metas, through the Outlook settings block |
In text templates
| Function | Signature | What it does |
|---|---|---|
content | content(c kit.Content, variant ...string) (string, error) | Renders content through the text block defines |
render | render(v kit.ChildView) (string, error) | Executes a nested component's own text.tmpl, trailing newlines trimmed; "" when it has none |
inline | inline(v kit.Inline, variant ...string) (string, error) | Renders an inline value as text |
wrap | wrap(s string) string | Wraps at 70 characters |
leaders | leaders(rows ...kit.Leader) string | Dot-leader rows, values in one column |
leader | leader(label, value string, sub ...string) kit.Leader | One dot-leader row, with at most one sub-value |
columns | columns(header []string, rows [][]string, right []bool) string | Padded columns |
cells, rows, bools | cells(v ...string) []string, rows(r ...[]string) [][]string, bools(b ...bool) []bool | Build 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:
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:
{{- /* 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:
@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
- Template checking: what the checker catches, and what it cannot.
- Email-safe HTML: the Outlook helpers and the lint.
Component schemas
component.yaml, key by key — attributes, bindings, content rules, children, ordering, limits and spacing — and the compile errors each rule produces.
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.