Skip to main content
A template is a React component plus the data it renders. This guide covers declaring templates with validated data, sharing parts between documents, and testing them.

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:
Invalid data rejects with a TemplateDataError listing every problem, before anything is rendered. With zod 4:
The messages come from your validator; 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:
Use it in any template, pass it different data, and change it in one place. Conditional or repeated parts are ordinary JSX: {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:

Preview while editing

Re-render on every save and convert to PDF with LibreOffice to review the layout:
LibreOffice is close to Word but not identical; check final layouts in Word.