Architecture overview
The Template Engine consists of several key components:- ModelManager - Loads and validates Concerto models
- TemplateMarkTransformer - Parses TemplateMark into an AST
- TemplateMarkInterpreter - Merges data with the template AST
- Markdown Transform - Converts output to various formats
The Template Engine is part of the broader Accord Project ecosystem, used for creating smart legal contracts and agreements.
Processing pipeline
The template processing happens in distinct phases:Phase 1: Model loading
The Concerto model is loaded and validated:store.ts:80-82, this process:
- Creates a ModelManager in strict mode for rigorous validation
- Adds the CTO model definition
- Fetches any external models referenced via imports
Strict mode ensures complete type checking and validation. Any model errors are caught at this stage.
Phase 2: Template parsing
The TemplateMark template is parsed into a DOM structure:store.ts:83-92, this:
- Creates a TemplateMarkTransformer instance
- Parses the markdown template into a TemplateMark DOM
- Validates template variables against the model
- Creates an intermediate representation for processing
- Variable placeholders
{{name}} - Conditional blocks
{{#if condition}} - Loop blocks
{{#ulist items}} - Clause blocks
{{#clause object}} - Formulas
{{% expression %}}
Phase 3: Data merging
The JSON data is merged with the template:store.ts:94-96, the engine:
- Parses the JSON data
- Validates data against the Concerto model
- Traverses the template DOM
- Replaces variables with actual values
- Evaluates conditionals and loops
- Executes formulas
- Produces a CiceroMark AST
Phase 4: Output transformation
The CiceroMark is transformed to the desired output format:store.ts:98-107, this:
- Converts CiceroMark to JSON
- Applies the markdown-transform library
- Generates the final HTML output
- HTML
- DOCX
- Plain text
- Markdown
Error handling
The Template Engine provides comprehensive error handling throughout the pipeline:store.ts:257-265, errors are caught and formatted for display.
Error types
Model validation errors
Model validation errors
Occur when the Concerto model has syntax errors or invalid type definitions.
Template parsing errors
Template parsing errors
Happen when TemplateMark syntax is incorrect or references undefined variables.
Data validation errors
Data validation errors
Triggered when JSON data doesn’t match the model structure.
Formula execution errors
Formula execution errors
Occur when TypeScript formulas throw exceptions.
Performance optimizations
The Template Playground implements several optimizations:Debounced rebuilds
store.ts:77, the rebuild function is debounced by 500ms. This prevents excessive reprocessing when you’re typing in the editor.
State management
The playground uses Zustand with Immer for efficient state updates:store.ts:163-402, this provides:
- Immutable state updates
- DevTools integration for debugging
- Selective re-rendering
Caching
The ModelManager caches external models to avoid redundant network requests:Template Engine API
The core engine exposes several methods:TemplateMarkInterpreter
modelManager- ModelManager instance with loaded modelsoptions- Configuration object (typically empty{})
TemplateMarkTransformer
template- Object withcontentproperty containing the template stringmodelManager- ModelManager instancekind- Template kind (typically"contract")options- Configuration object withverboseflag
Markdown Transform
ciceroMarkJson- CiceroMark document as JSON"ciceromark_parsed"- Source format identifier["html"]- Array of target formats{}- Transform optionsoptions- Additional options likeverbose
Real-world usage example
Here’s a complete example of using the Template Engine:Advanced features
Custom formatting functions
The engine supports custom formatters for dates and numbers:- Dates: Moment.js format strings
- Numbers: Numeral.js format strings
Formula context
Formulas execute with access to:Locale support
The join block supports internationalization:en, en-GB, fr, and others.
Integration with the playground
The playground wraps the Template Engine with additional functionality:Automatic rebuilds
Whenever you edit the template, model, or data:store.ts:267-279, the engine automatically reprocesses the template.
Error display
Errors are formatted and shown in the Problems panel:store.ts:407-418, complex error objects are flattened into readable messages.
Best practices
- Model validation first - Ensure your Concerto model is valid before testing templates
- Use strict mode - Always initialize ModelManager with
{ strict: true } - Handle errors gracefully - Wrap engine calls in try-catch blocks
- Debounce updates - Don’t trigger rebuilds on every keystroke
- Cache models - Reuse ModelManager instances when processing multiple templates
- Minimize formula complexity - Keep formulas simple and fast
- Test with real data - Use production-like data for testing
Debugging tips
Next steps
TemplateMark
Learn the template syntax the engine processes
Concerto models
Understand the models the engine validates against