Template Syntax
MessagePipe uses Handlebars templating engine with custom helpers for dynamic content generation. This allows you to create messages with conditional logic, loops and formatting.
Basic Variable Substitution
The simplest form of templating is variable substitution using double curly braces:
Variables object:
{
"firstName": "John",
"user": {
"email": "john@example.com"
}
}
Result:
Hello John!
Your email address is john@example.com.
Built-in Helpers
MessagePipe provides several built-in helpers for common formatting and logic operations.
Text Formatting Helpers
uppercase
Converts text to uppercase:
Hello JOHN!
lowercase
Converts text to lowercase:
capitalize
Capitalizes the first letter:
Welcome John!
default
Provides a fallback value if the variable is empty or undefined:
If firstName is empty or undefined, outputs: Hello Valued Customer!
Date Formatting Helper
formatDate
Formats dates in various formats:
Supported formats:
YYYY-MM-DD- ISO date formatMM/DD/YYYY- US date format- Default - Browser locale format
Conditional Helpers
ifEquals
Display content only if two values are equal:
ifNotEquals
Display content only if two values are NOT equal:
Standard Handlebars Features
MessagePipe supports all standard Handlebars functionality:
Loops with each
Variables:
{
"orders": [
{ "id": "12345", "date": "2024-01-15T10:30:00Z", "status": "shipped" },
{ "id": "12346", "date": "2024-01-10T14:20:00Z", "status": "delivered" }
]
}
Conditional Blocks with if
Nested Object Access
Complete Email Template Example
<!DOCTYPE html>
<html>
<head>
<title>Welcome to {{companyName}}</title>
<style>
.premium {
background: linear-gradient(45deg, gold, orange);
padding: 20px;
}
.basic {
background: #f5f5f5;
padding: 15px;
}
</style>
</head>
<body>
<h1>Welcome {{capitalize firstName}}!</h1>
<p>
Thanks for joining {{companyName}} on {{formatDate signupDate
"MM/DD/YYYY"}}.
</p>
{{#ifEquals accountType "premium"}}
<div class="premium">
<h2>Premium Member Benefits</h2>
<ul>
{{#each premiumFeatures}}
<li>{{this}}</li>
{{/each}}
</ul>
</div>
{{else}}
<div class="basic">
<h2>Getting Started</h2>
<p>
Here's what you can do with your {{capitalize accountType}} account:
</p>
<ul>
{{#each basicFeatures}}
<li>{{this}}</li>
{{/each}}
</ul>
</div>
{{/ifEquals}} {{#if hasOrderHistory}}
<h3>Your Recent Activity</h3>
<ul>
{{#each recentOrders}}
<li>{{this.item}} - {{formatDate this.date "MM/DD/YYYY"}}</li>
{{/each}}
</ul>
{{/if}}
<hr />
<p>
Best regards,<br />
{{default senderName "The Team"}}<br />
{{companyName}}
</p>
</body>
</html>
SMS Template Example
SMS templates are simpler but support all the same helpers:
Best Practices
Always Provide Defaults
Safe Property Access
Use Semantic Helpers
Keep SMS Templates Concise
Testing Templates
Preview Template
Use the preview endpoint to test your template with sample data:
curl -X POST "https://mzl-email-template-engine.dev.zero.mangozestlabs.com/templates/preview" \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"templateId": "your-template-id",
"variables": {
"firstName": "John",
"companyName": "Nobu",
"signupDate": "2024-01-15T10:30:00Z"
}
}'
Extract Variables
Get a list of all variables used in a template:
curl -X GET "https://mzl-email-template-engine.dev.zero.mangozestlabs.com/templates/variables/your-template-id" \
-H "Authorization: Bearer YOUR_JWT_TOKEN"
Returns:
["firstName", "companyName", "signupDate", "accountType"]
Common Template Errors
Invalid Handlebars Syntax
{
"statusCode": 400,
"message": "Template rendering error: Parse error on line 5",
"error": "Bad Request"
}
Solution: Check for unmatched {{#if}} blocks, missing {{/each}} closers, etc.