A simple TypeScript ESM template language for HTML emails
npm install @withstudiocms/template-lang
A simple TypeScript ESM template language for HTML emails, similar to Handlebars but focused on simplicity and database data integration.
- 🚀 Simple {{variable}} syntax for variable interpolation
- 🔍 Support for nested properties with dot notation ({{user.name}})
- ⚡ TypeScript with full ESM support
- 🛡️ Strict mode for error handling
- 🎯 Designed specifically for email templates and DB data
- 📦 Zero dependencies
``bash`
npm install @withstudiocms/template-lang
`typescript
import TemplateEngine from '@withstudiocms/template-lang';
const engine = new TemplateEngine();
const template = "Hello {{name}}! Welcome to {{company.name}}.";
const data = {
name: "John Doe",
company: {
name: "Acme Corp"
}
};
const result = engine.render(template, data);
console.log(result); // "Hello John Doe! Welcome to Acme Corp."
`
html
Hello {{user.firstName}} {{user.lastName}}!
Your order #{{order.id}} total is ${{order.total}}
`$3
`html
Shipping to: {{user.address.street}}, {{user.address.city}}
`API Reference
$3
#### Constructor
`typescript
new TemplateEngine(options?: TemplateOptions)
`#### Methods
render(template: string, data: TemplateData): string
Renders a template with the provided data.
compile(template: string): (data: TemplateData) => string
Compiles a template into a reusable function.
hasVariables(template: string): boolean
Checks if a template contains any variables.
getVariables(template: string): string[]
Returns an array of all variable names in the template.
setOptions(options: Partial
Updates the engine options.$3
`typescript
interface TemplateOptions {
strict?: boolean; // Throw error on missing variables (default: false)
defaultValue?: string; // Default value for missing variables (default: '')
}
`Examples
$3
`typescript
const emailTemplate = Your order #{{order.id}} has been confirmed.
Total: {{order.total}}
;const data = {
subject: "Order Confirmation",
user: { name: "John Doe" },
order: { id: "12345", total: "99.99" }
};
const html = engine.render(emailTemplate, data);
`$3
`typescript
const strictEngine = new TemplateEngine({ strict: true });// This will throw an error if 'missingVar' is not in data
try {
const result = strictEngine.render("Hello {{missingVar}}!", {});
} catch (error) {
console.log("Variable not found:", error.message);
}
`$3
`typescript
const engine = new TemplateEngine({ defaultValue: "[NOT SET]" });
const result = engine.render("Hello {{name}}!", {});
// Result: "Hello [NOT SET]!"
`$3
`typescript
// Compile once, use multiple times
const compiled = engine.compile("Hello {{name}}!");const result1 = compiled({ name: "Alice" });
const result2 = compiled({ name: "Bob" });
``Perfect for:
- HTML email templates
- Dynamic content generation from database data
- Simple templating needs without complex logic
- ESM-first TypeScript projects
MIT