Dynamic document assembly with .docx merge fields
Legal documents live in fragments before they become whole. You've got pre-approved clauses sitting in one place, witness statements in another, and regulatory annexes scattered across different files. With our latest enhancement to Document Templates, you can now merge the content of .docx files stored on a record directly into your generated documents, using a precise and highly controllable merge field syntax.
New merge field syntax
{{ record.field_<alias>[index].embedded }}With this syntax, you can embed the full content of a .docx file into a template, precisely where the merge field is placed.
That file can be:
- A pre-approved clause block
- A formatted cover page
- A pre-signed declaration
- Any other component your team has stored in a document field
This merge field reads the content from the .docx file and injects it directly into the generated document at that exact location, preserving all formatting, headings, spacing, styles, etc.
This opens up powerful use cases such as:
- Assembling client letters from pre-approved legal paragraphs
- Generating bespoke contracts by slotting in optional
.docxannexes - Dynamically inserting terms & conditions or disclaimers based on record logic
You have two options for how the .docx content is merged, depending on whether you want it inserted within a paragraph or to replace the paragraph entirely.
Merge options
Inline merge (default)
{{ record.field_annexes[0].embedded }}This places the .docx content within the paragraph where the merge field sits. Use this when embedding into a block of text, or where paragraph breaks are controlled externally.
Paragraph replacement (inline paragraph merge)
{{p record.field_annexes[0].embedded.inline }}This version replaces the entire paragraph where the merge field appears.
This is especially useful when:
- You want exact formatting from the inserted
.docxblock - You’re embedding long content such as multi-paragraph annexes
- You want to avoid nesting block elements inside other paragraphs
The {{p ... }} syntax ensures clarity and structure in the output document.
Important rules and validations
To ensure predictable behavior, keep the following in mind:
- Works only in Document Templates: If used elsewhere (e.g. Sequencer), the merge field returns an empty string.
- Supports only
.docxfiles: If the referenced file is not a.docx(e.g. PDF, TXT), the merge field resolves to an empty string. - Index is always required: Even if your field only holds one file, you must specify
[0]. Example:{{ record.field_clauses[0].embedded }}Omitting the index will return an empty result. - Out-of-range index? Empty result: If you refer to an index that doesn't exist (e.g.
[2]in a field with one file), the merge field returns an empty string.
In summary
Document assembly just became modular. Store your .docx building blocks in document fields, reference them through merge fields, and create templates that adapt to real-world needs without manual intervention.
No more copy-paste workflows. No more formatting inconsistencies. Just professional documents assembled exactly as you intended them.