Documents Templates and variables
Templates and variables
A variable is text between double braces, {{client}}. That is all a template is, so any DOCX can be one and Word shows it as it is: write it in Word, in the editor, or both, and fill it in the editor, on a server or from the command line.
Received from {{client | capital}} the amount of {{amount | currency}} on {{date | long date}}.
Received from Ana Pérez the amount of $2,500.00 on October 13, 2026.
Fill a template
A receipt with variables in its text, its header, a note at the foot of the page and its signatures. Type values in the panel and fill them, or have the page fill them as your application would, from a form.
Opening the editor…
Undo brings the variables back in one step, and the panel keeps what was typed. Open the sliders of a variable to change how it is written: the change is made to the text of the variable, {{amount | currency}}, so Word keeps it too.
Writing a template
A name between double braces, and after it, if it needs them, the formats its value is written in.
Letters of any language, digits and underscores, and then also spaces, dots and dashes: {{client}}, {{client.name}}, {{fecha de firma}}, {{客户}}. Spaces inside the braces are left out: {{ client }} is {{client}}.
Each after a bar, and applied from left to right: {{date | long date | upper}} writes the date, then puts it in capitals. A format it does not know is left out, and variableProblems names it.
The value takes the formatting of the text it stands in: a bold {{client}} is filled with a bold name. Word often breaks a word into pieces of the same look; the variable is found all the same.
The text and its tables, every header and footer (the first page’s and each section’s among them), the notes at the foot of the page and the text boxes.
Text taken out while changes are tracked is not part of a variable: what is read is what the document says. A variable put in while they were tracked gives its value as put in, by the same person.
{single}, {{}}, or a name with a sign in it such as {{a}b}} stay as they are. So does a variable given no value; one given an empty value is taken away.
Lists and conditions
What is between {{#name}} and {{/name}} is written once for each item of a list, or kept only when the value is there. Plain text still: Word shows the marks as they are.
The template
Order {{number}} for {{client.name}}{{#client.vip}} (priority){{/client.vip}}
{{#lines}}
{{lines.qty}} × {{lines.item}}: {{lines.price | currency}}
{{/lines}}
{{^lines}}
Nothing ordered yet.
{{/lines}}
The data
{
"number": 18,
"client": {
"name": "Ana Pérez",
"vip": true
},
"lines": [
{
"qty": 2,
"item": "Desk lamp",
"price": 45
},
{
"qty": 1,
"item": "Chair",
"price": 120
}
]
}What it makes
Order 18 for Ana Pérez (priority)
2 × Desk lamp: $45.00
1 × Chair: $120.00
Where the marks are says what is repeated
- A row of a table
- {{#lines}} in a cell, {{/lines}} in a cell of the same row. The row is written once for each item: the lines of an invoice, the people of a contract. A list without items takes the row away.
- Paragraphs
- {{#annex}} and {{/annex}} each a paragraph of its own. What is between them, tables included, is written once for each item, or kept or taken away. The paragraphs of the marks go.
- Text
- both marks in one paragraph. The text between them: {{#vip}}, our valued member{{/vip}}, or {{#tags}}{{tags}}, {{/tags}} for a list of words.
An item that is an object gives its fields by the name of the block: {{lines.price}}. An item that is text is the name itself: {{tags}}. Blocks go inside blocks, {{#orders.lines}} inside {{#orders}}, each with its own item.
A value that is not a list keeps the block once, or takes it away when it is false, empty, 0, "false" or "0", or a list without items. {{^name}} … {{/name}} is the other way round: for “nothing ordered”.
A block with no value stays as it is, marks and all, as a variable does: a document can be filled in steps.
A section break, a table inside the row to repeat, and marks that are neither in one paragraph, nor in one row, nor paragraphs of their own: those blocks stay as they are. Copies after the first leave out notes, bookmarks and floating pictures, which a document has once.
The receipt above repeats a row for each line of what it pays for, and keeps a paragraph only while it is not paid. The same data fills it on a server, from the command line or through an assistant.
Formats
What follows a bar says how the value is written. Type a variable and a value, and see each format applied in turn.
formatValue
$2,500.00
- the value2500
- | currency$2,500.00
What it gives
| Format | Value | en-US | es-MX | zh-CN | de-DE |
|---|---|---|---|---|---|
uppermayúsculas大写 | ana pérez | ANA PÉREZ | ANA PÉREZ | ANA PÉREZ | ANA PÉREZ |
lowerminúsculas小写 | ANA PÉREZ | ana pérez | ana pérez | ana pérez | ana pérez |
capitalnombre propio首字母大写 | ana pérez | Ana Pérez | Ana Pérez | Ana Pérez | Ana Pérez |
boldnegrita加粗 | Ana | Ana | Ana | Ana | Ana |
numbernúmero数字 | 1234567.5 | 1,234,567.5 | 1,234,567.5 | 1,234,567.5 | 1.234.567,5 |
number: 2número: 2数字: 2 | 1234.5 | 1,234.50 | 1,234.50 | 1,234.50 | 1.234,50 |
currencymoneda货币 | 2500 | $2,500.00 | $2,500.00 | $2,500.00 | $2.500,00 |
currency: EURmoneda: EUR货币: EUR | 2500 | €2,500.00 | EUR 2,500.00 | €2,500.00 | 2.500,00 € |
datefecha日期 | 2026-10-13 | 10/13/2026 | 13/10/2026 | 2026/10/13 | 13.10.2026 |
date: d MMMM yyyyfecha: d MMMM yyyy日期: d MMMM yyyy | 2026-10-13 | 13 October 2026 | 13 octubre 2026 | 13 十月 2026 | 13 Oktober 2026 |
long datefecha larga长日期 | 2026-10-13 | October 13, 2026 | 13 de octubre de 2026 | 2026年10月13日 | 13. Oktober 2026 |
full datefecha completa完整日期 | 2026-10-13 | Tuesday, October 13, 2026 | martes, 13 de octubre de 2026 | 2026年10月13日星期二 | Dienstag, 13. Oktober 2026 |
Every format is read by its English name, its Spanish one and its Chinese one (under it, in grey), with or without accents, whatever the language of the document; also by uppercase, mayúscula, lowercase, minúscula, title case, capitalizar, negritas, 粗体, cursivas, itálica, 斜体, fecha corta, 短日期, dateLong, dateFull, importe, 金额, picture and foto. italic and underline are written like bold. Set from the panel, a letter case takes the place of another, and so does a type of data: a variable is a date or a number, in one letter case.
A date is given as 2026-10-13, 13/10/2026 (day first, or month first in a language that writes it so), 2026年10月13日, or today / hoy / 今天. In a pattern after date:, d, dd, dddd (the day of the week), M, MM, MMM, MMMM, yy and yyyy are replaced and every other word is kept, so d de MMMM needs no quotes. A number is given plain, 1234.5; commas and a dollar sign in it are left out.
In a language
Values are written in the language of the document, or in English when it says none; give a locale for another. The names of months and days, the order of a date, the separators of numbers and the rules of capitals follow it.
fillVariables(doc, values); // 13 de octubre de 2026 · $2,500.00
fillVariables(doc, values, { locale: 'en-US' }); // October 13, 2026 · $2,500.00
fillVariables(doc, values, { locale: 'zh-CN', currency: 'CNY' }); // 2026年10月13日 · ¥2,500.00
fillVariables(doc, values, { locale: 'de-DE', currency: 'EUR' }); // 13. Oktober 2026 · 2.500,00 €- A currency of its own.
{{price | currency: EUR}}names its own, whatever is given to the call. - The editor writes values in the language of the text: the one chosen at its foot, its
languageprop, or the one the file says; else that of the interface with the browser’s region:editor.formatLocale. - The command line takes
--localeand--currency, and an assistant’sfill_variablestakeslocaleandcurrency. - The names of the formats the editor adds are in the language of its interface:
{{price | currency}}in English,{{price | moneda}}in Spanish,{{price | 货币}}in Chinese. All are read, whichever a document has.
In the editor
Variables stand out from the text around them, in the headers, footers and notes too. The Variables tab of the side panel is for whoever fills the template, not whoever wrote it; handle.showPanel('variables') opens it from your application.
Named so it reads: clientName, client_name and client.name all show as “Name”, under “Client” when other variables start with client. With more than six, a box to find one.
“Fill 3 variables in the document” puts every value typed in, as one step to undo; Enter in a box, or its ✓, fills that one alone. The values stay in the boxes, so undoing gives the variables back with them.
The sliders open the data type (text, date, long date, full date, number, currency, each shown as it will come out), the letter case, and bold, italic or underline. A number also takes its decimals, an amount its currency (EUR, MXN), a date a pattern (d MMMM yyyy). Under the box, what the value will come out as, or why it cannot.
The values from a .json file: { "client": { "name": "Ana" } } fills client.name, and a list is kept as it is for the block of its name.
Each block, with what it repeats: kept or taken out, or once for each item of a list loaded. Its marks stand out in the text like variables.
The target selects the next place the variable is used, in the header or a note too. A count says how many there are, when more than one.
A name and Insert puts it at the caret. Or select a word and press {{ }} Variable in the bar beside the selection; or Insert → Variable… in the toolbar.
For a form of your own in place of the panel, useDocsVariables gives the names, their formats and uses, a preview of a value, and fill.
Typing “{{”
Two braces bring up a list of the variables: those your application says it fills first, with what each is or a value it may have, then those the document already uses. What is typed after the braces narrows it, by any word of a name (cli finds client_name); Enter or Tab writes the one chosen whole, so a name is not mistyped and left unfilled. A name that is in neither is offered as a new variable. A closing brace, a # or a | leaves it, for a block or a format written by hand.
<DocsEditor
docx={file}
features={{
variableSuggestions: {
variables: [
{ name: 'client', title: 'Who signs' },
{ name: 'amount', example: '$1,200' },
],
},
}}
/>It is on in DocsEditor unless variableSuggestions: false; fromDocument: false leaves out the names of the document, allowNew: false the new one, and variables may be a function, asked each time the list opens. Headless it is the variableSuggestions option of DocsProvider, off unless given; without React, createVariableSuggest(editor, { variables }).
The Editor
- editor.variables
- The names of the variables of the document, in every part.
- editor.activeVariable
- The variable the caret is in, or the selection is.
- editor.blocks
- The blocks of the template, as templateBlocks gives them.
- editor.insertVariable(name)
- Puts {{name}} at the caret, or in place of the selection.
- editor.fillVariables(data)
- Fills them everywhere and writes out the blocks given a value, in the language of formatLocale, as one step to undo. While changes are tracked, the filling is tracked: each variable taken out, its value put in.
- editor.selectVariable(name)
- Selects the next place the variable is used after the caret, going round at the end: in the text, then the headers and footers, the notes and the text boxes, going into the one it is in.
- editor.variableFormats(name) · editor.variableUses(name) · editor.variableNotes(name)
- As in the core, on the document of the editor.
- editor.setVariableFormat(name, id, on, arg?)
- As in the core, written in the language of the interface, as one step to undo, and tracked while changes are.
- editor.formatLocale
- The language values are written in: the language of the text (chosen at the foot of the editor, the language prop, the one the file says, or the interface’s when the document was opened), with the browser’s region.
- editor.insertSignatures(titles?)
- Signatures at the caret, ADVISER and CLIENT when no titles are given, with the first name selected to type over.
In code
With @rebodocs/core, in the browser or on a server, without an editor.
import { importDocx, variableNames, variableProblems, fillVariables, exportDocx } from '@rebodocs/core';
const doc = importDocx(template);
variableNames(doc); // ['client', 'amount', 'date']
const values = { client: 'ana pérez', amount: '2500', date: '2026-10-13' };
variableProblems(doc, values); // [] — or { name, format, kind } for each that would go wrong
const docx = exportDocx(fillVariables(doc, values, { locale: 'en-US' }));- variableNames(doc) → string[]
- The names of the variables, each once, in the order they first appear: the text first, then the headers and footers, the notes and the text boxes.
- fillVariables(doc, values, { locale?, currency?, today? }) → Doc
- The document with each variable that has a value replaced by it, as its formats ask, in every part. A variable left out of values stays as it is, so a document can be filled in steps. The same document comes back when there was nothing to fill. Without a locale, values are written in the language of the document, or in English.
- variableProblems(doc, values, options?) → { name, format, kind }[]
- What would go wrong, before you fill: kind "format" for a format there is not, "value" for a value that is not what its format is for (not a date, not a number), which would be put as given.
- formatValue(value, formats, options?) → { text, props, problems }
- One value as its formats ask, without a document: the text, the bold, italic or underline they add, and what could not be applied. Without a locale, in English (en-US).
- findAllVariables(doc) → (Variable & { part? })[]
- Every use of every variable, in every part, with the part it is in (none for the text) and its range in that part: 'firstHeader', 'header@0' for a section’s own, 'footnote:1', 'textbox:7'.
- templateBlocks(doc) → { name, inverted, kind }[]
- The blocks of the template in every part, each once by its name and sign: kind is 'row', 'paragraphs' or 'text'.
- variableUses(doc, name) → number
- How many times a variable is used.
- variableNotes(doc) → Record<string, string[]>
- For each variable a note is on, or on a part of, what those notes say, in the order of the text: a description for whoever fills it.
- variableFormats(doc, name) → { id, arg }[]
- The formats every use of a variable has: what the panel shows as set.
- setVariableFormat(doc, name, id, on, arg?, language?) → Doc
- Gives a format to every use of a variable, or takes it away. A way of writing a date or a number takes the place of the one there was, and so does a change of capitals; bold, italic and underline add up. Written in the language given, or else the document’s: Spanish names when it starts with "es", Chinese ones when it starts with "zh", English ones otherwise or when there is none.
- formatText(id, arg?, language?) → string
- How a format is written in a document: formatText('date', 'dd/MM/yyyy') is "date: dd/MM/yyyy"; with 'es', "fecha: dd/MM/yyyy"; with 'zh-CN', "日期: dd/MM/yyyy".
- findVariables(doc) → Variable[]
- The variables of the paragraphs given, with where each is: { name, formats, range }. Give it a header as { paragraphs } to look in that part.
- variableText(name, formats?) · isVariableName(name)
- The text of a variable, {{name | format}}, and whether a name can be one.
- insertSignatures(doc, pos, titles?, name?) → { doc, pos, name? }
- A place to sign for each title, side by side, at pos: a table of one row without borders, each cell with the title, a line and a name under it, centred and bold. name is the range of the first name, to type over it.
From the command line
A template, a JSON file of values, and a DOCX, a PDF or Markdown out, by its extension. Nested values give dotted names: { "client": { "name": "Ana" } } fills {{client.name}}.
npx -p @rebodocs/mcp rebodocs-generate template.docx --list
npx -p @rebodocs/mcp rebodocs-generate template.docx data.json -o out.docx --locale en-US
npx -p @rebodocs/mcp rebodocs-generate template.docx data.json -o out.pdf --fonts ./fontsIt warns of the variables left without a value and of the values a format cannot take. Every option is on the page On a server.
For an assistant
The MCP server has a tool for each step, on a file or on the document open in an editor.
- list_variables
- Each variable with how many times it is used and the formats it is given, and each block with what it repeats.
- fill_variables { values, locale?, currency? }
- Fills them everywhere, lists and yes or no for the blocks among the values, and says how many it filled, which are still without a value, which names are not variables of the document, and which values a format could not write.
- insert_signatures
- Signature lines after a paragraph, with the titles and names given.
Areas to fill in
A document restricted to some areas: whoever has it can change those and nothing else, as Word’s Restrict editing with exceptions for everyone. A template sent to be filled in, with its clauses as they were written.
Outside the areas the document is read, copied and noted on; a change there is not made, the areas flash, and a screen reader is told why. Tab and Shift+Tab go from one area to the next, selecting what it holds. An area may be empty, a blank shown as a small box. The areas are shaded on the page, more strongly while the document is restricted.
editor.setSelection(from, to);
editor.makeEditable(); // the selection, or an empty blank at the caret
editor.setRestricted(true); // only the areas can change now
const docx = editor.exportDocx(); // and Word restricts it the sameIn the file the areas are the marks Word writes (w:permStart and w:permEnd for everyone), and the restriction is w:documentProtection w:edit="readOnly" without a password: Word lets only the areas be changed, and its Restrict editing pane can stop it. A Word file restricted this way opens restricted here.
With features.forms the interface marks them: Mark the selection as editable, Remove editable area and Restrict editing in the Review menu, Editable in the bar beside a selection, and Editable area in the “/” menu. It is off unless true, so that whoever is sent a restricted document cannot open it again; the restriction holds without it, and your code decides. Headless it is the forms option, and state.restricted and state.activeEditable say where things stand.
- editor.makeEditable()
- Makes the selection an area that can be changed, or the caret an empty one to write in, and returns its id. Not while restricted.
- editor.removeEditable(id?)
- Takes away that area, or the one the caret is in; its text stays. Not while restricted.
- editor.editableRanges · editor.activeEditable
- The areas, each with its id and range, in their order; the id of the one the caret is in.
- editor.setRestricted(on) · editor.restricted
- Restricts the document to its areas, or opens it all again. It is said in the file; it is not a step to undo.
- editor.nextEditable(dir?)
- Selects what the next area holds, or the one before with -1, going round: what Tab and Shift+Tab do while restricted.
- editor.onRefused
- Called when a change outside the areas is not made, for your interface to say so as well.
A restricted document refuses your code too: open it with setRestricted(false) before filling its variables outside the areas, and restrict it again after. Each person in a shared room has it restricted in their own copy, so a change made by someone with it open still comes in.
Signatures
A block of signature lines side by side, at the caret: a title, a line and a name under each, in a table without borders. The receipt above has one.
In the editor, Signatures in the toolbar’s Insert menu or in the bar beside the caret; in code, editor.insertSignatures(['Landlord', 'Tenant']). Put variables in the names, {{tenant | upper}}, and they fill with the rest. Not inside a table.
What to know
- A value is one line: line breaks in it become spaces.
todayis the day of the machine that fills: on a server, give{ today }for the day of the person.- A variable is plain text to Word: it can be found, replaced and spell-checked there like any other word.