Declare a template
name and version are recorded in every document the template renders, with a hash of the data; see Audit and provenance. Bump version when the output changes.
schema accepts any Standard Schema validator: zod, valibot, arktype and others. React DOCX does not depend on any of them. With a schema, the data type is inferred from it.
Render with data from a request, a queue or a file:
TemplateDataError listing every problem, before anything is rendered. With zod 4:
error.issues holds them with their paths.
Templates without a schema are typed by their render parameter, and renderTemplate checks the data type at compile time.
Share parts between documents
Shared parts are components. A header used by every document of a company:{items.map(...)} and {condition && ...}.
Data: props or variables
Prefer passing data as props, as above: TypeScript checks every use.<Variable> and useVariables() read values passed in the render options; they suit a few document-wide values. A <Variable> that was not provided rejects the render, so a missing value never produces a silently incomplete document.
Test templates
@cordel/react-docx/testing reads a rendered document back:
Rendering is deterministic: the same template and data produce byte-identical files. Snapshot
docx.document to catch any change in a template’s output:
