Component schemas
Every component has a schema, components/<name>/component.yaml. It says what the component accepts in a Markdown template: which attributes, which Go values, which Markdown content, which nested components, where it may sit and how much space goes around it. lttr checks every template against these schemas when it compiles, so a template that breaks a rule never renders.
Read a whole schema
This is the tutorial kit's invoice component:
# An invoice: the bound Invoice's lines, then its total. A template
# passes the invoice with a binding: ::invoice{:invoice="invoice"}.
name: invoice
attrs:
title: { type: text, default: Invoice }
bindings:
invoice: { type: Invoice, required: true }
content: none
spacing:
top: size.block
bottom: size.gap
A template uses it with a binding and no content:
::invoice{:invoice="invoice"}
::
titleis an optional text attribute that defaults toInvoice.invoiceis a required binding: a Go value of the registered typeInvoice.content: none: anything between::invoiceand::is a compile error.- The spacing puts
size.blockabove the component andsize.gapbelow it.
Know the keys
| Key | What it sets |
|---|---|
name | Optional. When present, it must equal the directory name |
aliases | Other names for the component, such as [prose]. Names match [a-z][a-z0-9_-]*, and no two components may share one. Lookup is case-insensitive |
class | A label that spacing.after rules match as .class |
attrs | Typed attributes, ::name{key="value"} |
bindings | Go values, ::name{:key="path"} |
content | The Markdown it holds: none (the default when the key is absent), blocks, or a mapping of rules |
children | One kind of nested component, with counts. A child may have children of its own |
must_follow | The components it must directly follow |
limits | Caps on :button and :link inside its content |
spacing | Its padding, with rules for its position and neighbours |
Any other key is a load error, as in kit.yaml.
Type the attributes
Each attribute has a type and, optionally, required, required_with, default or values. Plain's image component has required, optional and defaulted attributes:
# A full-width image (or narrower with width), with an optional link and
# caption.
name: image
attrs:
src: { type: url, required: true }
alt: { type: text, required: true }
# Width in px. The default and the cap in html.tmpl are size.content in
# kit.yaml (templates cannot compare an int with a token); change all
# three together.
width: { type: int, default: "496" }
href: { type: url }
caption: { type: text }
content: none
spacing:
top: 0
bottom: size.gap
| Type | In the template as | What the Markdown may write |
|---|---|---|
text | string | Text with merge tags |
url | template.URL | A URL with merge tags. It is evaluated and checked (https, http, mailto or tel), and a merge value cannot move its host |
int | int | A literal integer |
flag | bool | The bare attribute ({wide}), or true or false |
enum | string | One of values |
list | []string | A comma-separated literal |
inline | kit.Inline | One paragraph of Markdown, merge tags allowed. Render it with {{inline .Attrs.note}} |
html | template.HTML in html.tmpl, string in text.tmpl | One merge tag naming an lttr.TrustedHTML field of typed data: HTML the application rendered. See Pass trusted HTML |
required: true: the template must set it.required_with: other: it must be set whenotheris. It cannot be combined withrequired.default: a literal valid for the type (an integer forint,trueorfalseforflag, one of thevaluesforenum). A required attribute has no default, and neither has anhtmlone.values: the choices of anenum, and only anenum.- Unset, an attribute is its default, or else the zero value of its type, so a template can test it with
{{if .Attrs.href}}. - An optional
urlthat should be unset in some sends needs an explicit empty fallback in the template,href="{{link |}}". See Merge tags and data.
Attribute names are identifiers: letters, digits and _, not starting with a digit. Plain's footer takes one inline attribute:
# The closing rule, an optional note, the sender's company and address
# (kit vars company and address; override them with Config.Vars) and, when
# the recipient has one, an unsubscribe link.
name: footer
attrs:
note: { type: inline }
content: none
spacing:
top: 8
bottom: size.gap
Bind Go values
bindings pass typed Go values from the template's data into the component. Each binding has a type, which must be a name in kit.yaml's types, and optionally required: true. In Markdown a binding is written :key="path", a path into the template's data, as in ::invoice{:invoice="invoice"} above.
The value must have exactly the registered type: a missing required binding, a value of another type, or untyped map[string]any data is a compile error. An optional binding the template does not set is nil in the view, so guard it with {{with .Bind.x}}. Go types and funcs covers registration and the errors.
Say what content it takes
content is one of three forms:
none, or nocontentkey: the component takes no Markdown.blocks: paragraphs, headings, lists, action rows and tables.- A mapping of rules. A count it leaves out is unlimited.
Plain's default component allows images in prose, strikethrough, three heading levels and table headers:
# The default component: loose Markdown outside any ::component lands here.
name: section
content:
allow: [paragraph, heading, list, actions, table, image]
inline: [strike]
headings: [1, 2, 3]
table_header: true
spacing:
top: 0
bottom: size.block
| Rule | Meaning |
|---|---|
allow | The block kinds allowed: paragraph, heading, list, actions, table and image. Without allow, the first five. Images are opt-in |
inline | Opt-in inline features: strike (~~text~~) and link_title ([text](url "title")) |
headings | The heading levels allowed, 1 to 6. Absent: every level |
min_paragraphs, max_paragraphs | How many paragraphs |
min, max | How many top-level blocks |
max_columns | The most columns a table may have |
table_header | Treat a table's first row as its header row (BlockView.Header). Otherwise it is the first body row |
max_kind | Caps per block kind, such as max_kind: { heading: 1 } |
sequence | The exact kinds of the top-level blocks, in order, such as sequence: [heading, paragraph] |
Every kind named in max_kind or sequence must be in allow. Raw HTML is never allowed, and neither are block quotes.
Nest children
children lets a component hold one kind of nested component, written with one more colon than its parent. Plain's columns holds two or three columns:
# Two or three side-by-side columns that stack on phones. Each column is a
# :::column child holding ordinary Markdown.
name: columns
content: none
children: { component: column, min: 2, max: 3 }
spacing:
top: 0
bottom: size.block
componentnames the child. The child may declarechildrenof its own, and so on down: a::stepsof:::steps, each holding a::::tip. Nesting is bounded only by the parser's 32 levels, and a cycle (aholdsb,bholdsa) is allowed.minandmaxcount the children. Absent, they are unlimited.- Any nested component inside it must be its child. With
content: none, the default, it holds only its children, and any other Markdown is a compile error (columns: only component column is allowed here). A component with bothchildrenand acontentrule can mix Markdown and children. - Every level is checked against its own schema, and an error inside a child names that child at its own line. From a copy of Plain with a
stepscomponent whosestepchildren each take at most onetip:
$ lttr lint -kit ./emailkit templates/steps.md
templates/steps.md:11:1: tip: at most 1 paragraph, got 2
templates/steps.md:17:1: step: only component tip is allowed here
templates/steps.md:22:1: step: missing "title"
templates/steps.md:22:1: step: unknown attribute "titel" (did you mean title?)
- The parent's template reads the children as
.Children, each with its own attributes, bindings, content, children and spacing, and can render each one with its own template throughrender. See Render nested components.
Pass trusted HTML
Markdown authors can't write HTML: raw HTML in a template is always a compile error. An application that already has HTML to show (a formatted report, a comment rendered by its own sanitiser) passes it from Go, through an attribute of type html:
# HTML the application rendered (a comment, a formatted report), passed in
# from Go as an lttr.TrustedHTML.
name: excerpt
attrs:
body: { type: html, required: true }
content: none
spacing:
top: 0
bottom: size.block
The value comes from a field of type lttr.TrustedHTML in the template's data, which carries the HTML and its plain-text alternative:
type TrustedHTML struct {
HTML template.HTML
Text string
}
A data type with such a field, type Digest struct { Summary lttr.TrustedHTML `json:"summary"` }, fills it in Go:
d := Digest{Summary: lttr.TrustedHTML{
HTML: template.HTML(`<p style="margin:0;">Three new <strong>orders</strong> and one refund.</p>`),
Text: "Three new orders and one refund.",
}}
The template names the field with one merge tag and nothing else, ::excerpt{body="{{summary}}"}. In html.tmpl, .Attrs.body is the template.HTML, written as is; in text.tmpl it is the Text string:
<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"}}">{{.Attrs.body}}</td></tr>
lttr does not sanitise the HTML. It is inserted exactly as given, so its safety is your application's responsibility: never build it from user input without sanitising it first. The lint (lttr lint, lttr kit check, or lint.Lint and lint.Check from Go) still runs over the rendered email and reports markup email clients mishandle, such as a <div style="display:flex"> inside it:
css contains "flex" at byte 5902
<div> at byte 5902: style has display:flex
The lint is not a sanitiser: it does not catch event handlers such as onclick, or javascript: URLs, in trusted HTML. Sanitise before the value reaches lttr.
Everything else about the value is checked, at compile time with a position unless noted:
| Template or data | Error |
|---|---|
A literal value, body="<b>Hi</b>", or text around the tag, body="Read: {{summary}}" | excerpt: body: html attribute takes one merge tag, e.g. body="{{path}}" |
A fallback, body="{{summary | none}}" | excerpt: body: html attribute takes a merge tag without a fallback, e.g. body="{{summary}}" |
A field of another type, body="{{note}}" | excerpt: body: {{note}} is string, want lttr.TrustedHTML |
A pointer field, *lttr.TrustedHTML | excerpt: body: {{ptr}} is *lttr.TrustedHTML, want lttr.TrustedHTML |
Untyped map[string]any data | excerpt: body: needs typed data (lttr.TrustedHTML) |
A misspelt path, body="{{sumary}}" | unknown name "sumary" (did you mean summary?) |
A merge tag anywhere else that reads into the value, {{summary.html}} | {{summary.html}} is inside lttr.TrustedHTML; pass it through an html attribute |
At render, HTML set and Text empty | excerpt: body: trusted html needs a text version |
At render, a required attribute whose HTML and Text are both blank | excerpt: body: no value for {{summary}} |
A value with Text but an empty HTML renders its text in the text part only, and nothing in the HTML. An html attribute can't have a default (attrs.intro: an html attribute has no default), and a kit frontmatter key can't be of type html (frontmatter.banner: html is not allowed here), since both would come from YAML rather than Go. An optional html attribute that isn't given, or a zero TrustedHTML, renders nothing. A merge tag can't reach into a TrustedHTML ({{summary}} is {{summary}} is lttr.TrustedHTML, not a scalar, and {{summary.text}} is refused as above): its HTML and text reach an email only through an html attribute, so the two parts always come from the same value. It works the same on a nested child, read inline or through render. Neither shipped kit declares an html attribute.
Cap actions and set the order
must_follow: [statement]: the component must come directly after one of the named components.limits: { button: 1, link: 2 }: caps the:buttonand:linkinline components inside this component's content. A cap of0forbids them.kit.yamlhas the same key for the whole template.aliases: [t1]: lets a template write::t1for the component.
Space the sections
Each component draws its own section, and spacing decides its top and bottom padding. Plain's button removes the gap between itself and a section or column above it:
# A standalone square button: full width on phones.
name: button
attrs:
href: { type: url, required: true }
label: { type: text, required: true }
content: none
spacing:
top: 0
bottom: size.gap
after:
section: { top: 0, prev_bottom: 0 }
column: { top: 0, prev_bottom: 0 }
| Key | Meaning |
|---|---|
top, bottom | The padding, in px or a size token |
first | {top, bottom} when the component is first in the document |
last | {top, bottom, prev_bottom} when it is last |
after | Per previous component, by name or .class: {top, bottom, prev_bottom} when this component directly follows it |
prev_bottom changes the bottom padding of the component before. lttr resolves spacing in three passes, and later passes win:
- Every component takes its
topandbottom. - In document order, each component applies its
afterrule for the component before it: first by that component's name, else by its.class. - The first component applies
first, then the last applieslast.
The result reaches the template as .Section.Top and .Section.Bottom, which Plain writes as the cell padding: padding:{{px .Section.Top}} … {{px .Section.Bottom}}. Children are spaced the same way among their siblings.
Read the errors
A schema problem is a load error, at its position in component.yaml. A template that breaks a schema is a compile error, at its position in the Markdown. These come from lttr lint against Plain:
$ lttr lint -kit plain templates/welcome.md
templates/welcome.md:7:1: section: h4 is not allowed here
templates/welcome.md:9:1: button: missing "href"
templates/welcome.md:12:1: columns: 2 to 3 column components, got 1
templates/welcome.md:18:1: footer: unknown attribute "colour"
And these from a copy of Plain with must_follow: [section] on button, sequence: [heading, paragraph, actions] on column and a campaign frontmatter key:
$ lttr lint -kit ./emailkit templates/spring.md
templates/spring.md:5:1: frontmatter: "campaign" must be one of spring, autumn
templates/spring.md:21:1: column: expected a heading then a paragraph then an action row
templates/spring.md:26:1: column: expected a heading then a paragraph then an action row
templates/spring.md:17:1: button must directly follow a section
Compile errors lists the messages from the template's side.
Next steps
- Templates and views: what the component's templates receive.
- Go types and funcs: register the types bindings name.