Document Helpers
@fabricadabra/doc — 0.1.0 (#1092)
Shared helpers for document-generation templates (invoices, receipts, contracts). Platform-provided: the server materialises this file into the compile root of every Typst run, so a template can import it without the account carrying a copy. Ask for “eject library” (#1095) to get an editable copy in your own project.
VERSIONED BY FILENAME, AND PINNED FOREVER. A template importing
0.1.0.typ keeps rendering exactly as it does today: a later platform
release adds 0.2.0.typ beside this file rather than changing it. Nothing
here may change behaviour once published — a document is a sent invoice
or a signed contract, and a reprint that quietly differs from what the
client received is the failure this rule exists to prevent. Fix bugs in a
new version and let templates opt in.
Scope is deliberately helpers only, no layout: templates own their own tables and page furniture, so adopting this file never constrains how a document looks.
doc-data
Section titled “doc-data”#let doc-data(placeholder: (:))The document-generation pipeline supplies the whole shaped payload as a
single JSON data input. A plain Run in the editor supplies only the
script’s declared frontmatter params, so data is absent there and an
unguarded read kills the template before it renders anything (#1033).
Pass a placeholder of the right shape to get a skeleton render instead.
Templates keep their own placeholder rather than taking one from here, so
it stays next to the header comment documenting the shape it mirrors.
Parameters:
| Name | Type | Description |
|---|---|---|
placeholder |
dictionary |
Value returned when data is absent, i.e. on a plain editor Run. Give it the same shape the real payload has so the template renders a skeleton instead of failing on a missing key. |
Returns: dictionary — The decoded data payload during document generation, or
placeholder outside it.
custom-param
Section titled “custom-param”#let custom-param(data, name, default: (), decode: true)A template’s own frontmatter params arrive by two different routes, and reading only one makes the declared control silently do nothing in the other context — no error, just a missing section (#1033):
document generation -> nested under data.custom (#595) a plain editor Run -> a top-level input, –input name=…
Top-level wins, then data.custom, then the default.
Structured (array/object) params arrive JSON-encoded; a plain text or
number param arrives raw, so pass decode: false for those.
Parameters:
| Name | Type | Description |
|---|---|---|
data |
dictionary |
The payload from doc-data, whose custom dict is consulted when no top-level input of this name was supplied. |
name |
str |
The frontmatter param’s declared name. |
default |
— | Value used when the param appears in neither route. |
decode |
bool |
Whether to JSON-decode the value. Structured (array/object) params arrive encoded and need this; pass false for plain text and numbers. |
Returns: The param’s value from sys.inputs, else data.custom, else default.
doc-theme
Section titled “doc-theme”#let doc-theme(data, fallback: rgb("#2b6cb0"))The business’s brand colour, falling back when the account hasn’t set one.
colours is always present on a shaped payload but may be an empty dict.
Parameters:
| Name | Type | Description |
|---|---|---|
data |
dictionary |
The payload from doc-data. |
fallback |
color |
Colour to use when the account has set no brand colour. |
Returns: color — The business’s primary brand colour, or fallback.
fmt-money
Section titled “fmt-money”#let fmt-money(x)Money values arrive as major units (not cents) already scaled by the pipeline, so this only formats — it never rescales.
Parameters:
| Name | Type | Description |
|---|---|---|
x |
int | float |
Amount in major units, already scaled by the pipeline. |
Returns: str — The amount with a currency symbol, to two decimal places.
fmt-address
Section titled “fmt-address”#let fmt-address(addr)An address is either a preformatted string or a dict of optional parts; absent and empty parts are skipped rather than rendered as blanks.
Parameters:
| Name | Type | Description |
|---|---|---|
addr |
str | dictionary | none |
A preformatted address string, or a dict of optional line1/line2/city/region/postcode/country parts. |
Returns: str \| none — The address as a single comma-separated line, or none when
addr is none.