A TypeScript library for applying template data to Nutrient Document Authoring documents. Populate DOCX templates with dynamic data using a Mustache-like syntax for placeholders, loops, and conditionals.
- 📝 Simple Placeholders - Replace
{{variable}}with data values - 🔄 Loops - Duplicate content for arrays using
{{#array}}...{{/array}} - ✅ Conditionals - Show/hide content with
{{#condition}}...{{/condition}} - ❌ Negated Conditionals - Invert conditions with
{{^condition}}...{{/condition}} - 📊 Table Support - Automatically duplicate table rows for array data
- ⚙️ Custom Delimiters - Configure your own placeholder syntax
- 🎨 Formatting Preservation - Maintains text formatting (bold, italic, fonts, etc.)
- 🔧 TypeScript - Full type safety and IDE autocomplete
- Node.js 18 or later
- A Nutrient Document Authoring license key (optional - will show watermark without one)
npm installnpm run devThen open http://localhost:3000 in your browser.
import { applyTemplateData } from './src/index.ts';
// Your template data
const templateData = {
config: {
delimiter: { start: '{{', end: '}}' },
defaultValue: ''
},
model: {
customerName: 'Acme Corporation',
invoiceNumber: 'INV-2024-001',
total: '$12,420.00',
lineItems: [
{ description: 'Web Development', quantity: '40', amount: '$6,000.00' },
{ description: 'UI/UX Design', quantity: '20', amount: '$3,500.00' }
]
}
};
// Apply template data to an open document
await applyTemplateData(docInstance, templateData);Replace with a value from your data model:
Invoice Number: {{invoiceNumber}}
Customer: {{customerName}}
Total: {{total}}
Duplicate content for each item in an array:
{{#lineItems}}
- {{description}}: {{amount}}
{{/lineItems}}
For tables, the loop duplicates rows:
| Description | Quantity | Amount |
|-------------|----------|--------|
| {{#lineItems}}{{description}} | {{quantity}} | {{amount}}{{/lineItems}} |
Show content only if a condition is true:
{{#showDiscount}}
*** SPECIAL DISCOUNT APPLIED ***
Discount: {{discountAmount}}
{{/showDiscount}}
Show content only if a condition is false:
{{^isPaid}}
*** PAYMENT DUE ***
Please pay by {{dueDate}}
{{/isPaid}}
document-authoring-template-population/
├── src/
│ ├── index.ts # Main library export
│ ├── lib/
│ │ ├── document-templating.ts # Core templating engine
│ │ ├── parser/
│ │ │ └── template-parser.ts # Placeholder parsing
│ │ ├── processors/
│ │ │ ├── placeholder-replacer.ts # {{variable}} replacement
│ │ │ ├── conditional-processor.ts # Conditional logic
│ │ │ └── loop-processor.ts # Array loop handling
│ │ └── utils/
│ │ ├── data-resolver.ts # Data access utilities
│ │ └── validator-util.ts # Validation helpers
│ └── types/
│ └── index.ts # TypeScript type definitions
├── index.html # Demo application
├── package.json
├── tsconfig.json
├── vite.config.js
└── README.md
The library processes templates in three phases:
- Loop Processing - Duplicates content (including table rows) for array data
- Conditional Processing - Shows or hides content based on boolean values
- Placeholder Replacement - Replaces simple
{{variable}}placeholders with values
All operations happen within a Document Authoring transaction for atomic updates.
When a loop ({{#array}}...{{/array}}) is detected in a table row:
- The library identifies the template row
- Creates new rows for each array item
- Replaces placeholders in each row with item data
- Preserves text formatting (font, size, bold, etc.)
- Removes the template row
Note: Cell-level formatting (borders, backgrounds) uses default styling as the Document Authoring API doesn't currently expose methods to copy these properties.
Applies template data to a Document Authoring instance.
Parameters:
instance- The Document Authoring document instancedata- Template data object containing:config.delimiter- Placeholder delimiters (default:{{ }})config.defaultValue- Value for missing data (default:'')model- Your data object
options- Optional configuration (currently unused)
Returns: Promise<void>
Example:
const data = {
config: {
delimiter: { start: '{{', end: '}}' },
defaultValue: 'N/A'
},
model: {
name: 'John Doe',
items: [...]
}
};
await applyTemplateData(doc, data);Use different placeholder syntax:
const data = {
config: {
delimiter: { start: '[[', end: ']]' } // Use [[variable]] instead
},
model: { ... }
};Set a fallback for missing data:
const data = {
config: {
defaultValue: 'N/A' // Show 'N/A' instead of empty string
},
model: { ... }
};- Table Formatting: New rows created by loops will have default borders/backgrounds as the API doesn't expose cell formatting properties
- Nested Loops: Not currently supported
- Complex Expressions: Only simple value lookups are supported (no calculations or filters)
This example is licensed under the MIT License.
For issues with this example:
- Create an issue in the awesome-nutrient repository
For Nutrient Document Authoring SDK support:
- Visit Nutrient Support
- Check the Document Authoring documentation
Contributions are welcome! Please feel free to submit a Pull Request.