Template engine
The template engine in Compozy uses Jinja2-style syntax with JavaScript expressions, allowing you to reference and manipulate data throughout your workflows.
Introduction
The template engine is a core feature of Compozy that allows you to:
- Reference data from various workflow components
- Perform dynamic calculations and transformations
- Access environment variables and configuration
- Handle conditional logic inline
Compozy uses Jinja2-style syntax combined with Deno for JavaScript evaluation, providing:
- Simple expressions using
{{ ... }}
for single-line JavaScript - Complex logic using
$exec: |
for multiline JavaScript code
Since we use Deno runtime, all JavaScript code in templates follows modern ECMAScript standards and has access to Deno’s built-in security features.
Template Syntax
Simple Expressions
For basic operations, use the {{ ... }}
syntax. These expressions should be concise and focus on a single operation:
Multiline Code
For complex operations that require multiple lines or intermediate variables, use the $exec: |
syntax:
Working with Objects and Arrays
Built-in Functions
Compozy includes several popular libraries for common operations. Here are some examples:
For a complete reference of available functions, see Template Functions.
Custom Dependencies
You can extend the template engine with additional libraries. All custom dependencies are automatically namespaced under the $
object:
Learn more about adding custom dependencies in Custom Dependencies.
Best Practices
Keep Expressions Simple
Use simple expressions for basic operations:
Move complex logic to multiline blocks:
Use Optional Chaining
Always use optional chaining for nested properties:
Handle Errors
Implement proper error handling in complex operations:
Type Safety
Use explicit type conversions:
Next Steps
Template Functions
Explore built-in utility functions
Custom Dependencies
Learn about adding custom libraries
References
- Jinja2 Template Engine - The template syntax inspiration
- Deno Runtime - The JavaScript runtime used for expression evaluation
- Lodash Documentation - Utility library documentation
- Day.js Documentation - Date manipulation library
- Voca Documentation - String manipulation library
- Numeral.js Documentation - Number formatting library