Skip to content
@deckyard/docs

Templates

A template is a DOCX or ODT made in an office suite with {{name}} placeholders. Filling it patches the file: only the text that held a placeholder changes, so styles, headers, fields, pictures and everything else the model cannot hold stay as they were.

jennifer
import "@deckyard/docs/" as docs;
use fs;

def tpl as bytes init fs.readBytes("invoice.docx");
def items as list of map of string to string init [{"name": "Pen", "qty": "2"}, {"name": "Ink", "qty": "1"}];
fs.writeBytes("invoice-7.docx", docs.fillDocxWith($tpl, {"number": "7", "customer": "Ada"}, {"items": $items}));

Rules

RuleBehaviour
NameA letter or _, then letters, digits, _, . or -; spaces inside the braces are allowed ({{ name }})
Not a name{{ two words }} is left as text
Split runsAn editor often splits {{name}} over several runs; it is found in the paragraph's text all the same
FormattingThe value takes the formatting of the placeholder's first character
Line ends and tabsA line end in a value becomes a line break, a tab a tab; in ODT runs of spaces are kept
Missing valueEvery placeholder without a value is listed in one "docs" error
RowsA table row holding {{key.field}} for a key of rows is repeated once per record; no records removes the row
WhereDOCX: body, headers, footers, footnotes, endnotes. ODT: body and the styles part (headers and footers)
MarkupComments, CDATA sections and processing instructions are copied through untouched; an attribute value may hold the other quote character (w:val="Client's name")
ValuesA value holding a character XML forbids (C0 controls other than tab and line end, U+FFFE, U+FFFF) is refused with "docs"

placeholders(b) lists the names a template holds, in order of first use, to check a template against the data before filling it.