Best Practices for Template Authors¶
This guide provides practical tips and recommendations for creating maintainable, efficient, and error-free templates.
Naming Conventions¶
Use Descriptive Names¶
❌ Bad:
✅ Good:
Use Consistent Naming Styles¶
Pick a style and stick with it throughout your template:
PascalCase (recommended):
camelCase:
Avoid:
- snake_case (hard to read in templates)
- kebab-case (doesn't work well with dot notation)
- Mixing styles
Boolean Names Should Be Questions¶
✅ Good:
❌ Less clear:
JSON Structure¶
Group Related Data¶
❌ Flat structure:
{
"CustomerName": "Alice",
"CustomerEmail": "alice@example.com",
"CustomerPhone": "+1-555-0123",
"ShippingStreet": "123 Main St",
"ShippingCity": "Springfield",
"ShippingState": "IL",
"BillingStreet": "456 Oak Ave",
"BillingCity": "Chicago",
"BillingState": "IL"
}
✅ Nested structure:
{
"Customer": {
"Name": "Alice",
"Email": "alice@example.com",
"Phone": "+1-555-0123"
},
"ShippingAddress": {
"Street": "123 Main St",
"City": "Springfield",
"State": "IL"
},
"BillingAddress": {
"Street": "456 Oak Ave",
"City": "Chicago",
"State": "IL"
}
}
Match JSON to Template Usage¶
Structure your data to match how you'll use it in the template.
Template needs:
{{#foreach Departments}}
Department: {{Name}}
Employees:
{{#foreach Employees}}
- {{Name}}
{{/foreach}}
{{/foreach}}
JSON structure should match:
{
"Departments": [
{
"Name": "Engineering",
"Employees": [
{ "Name": "Alice" },
{ "Name": "Bob" }
]
}
]
}
Template Organization¶
Add Comments in Word¶
Use Word's comment feature to document your templates:
- Select a placeholder or section
- Right-click → New Comment
- Explain what data is expected or what the section does
Example comments: - "This section only shows for VIP customers" - "Expected format: YYYY-MM-DD" - "Loop expects array of products with Name, Price, SKU"
Break Complex Templates into Sections¶
Use clear section headings in your Word document:
=== CUSTOMER INFORMATION ===
{{Customer.Name}}
...
=== ORDER DETAILS ===
{{#foreach Items}}
...
{{/foreach}}
=== PAYMENT INFORMATION ===
...
Keep Line Length Reasonable¶
❌ Hard to read:
{{#if Status = "Active" and HasSubscription and not IsExpired and DaysRemaining > 0 and AccountType = "Premium"}}...{{/if}}
✅ Easier to read:
{{#if Status = "Active" and HasSubscription}}
{{#if not IsExpired and DaysRemaining > 0}}
{{#if AccountType = "Premium"}}
Premium content here
{{/if}}
{{/if}}
{{/if}}
Testing¶
Start with Simple Test Data¶
Use obvious test values to verify placeholders work:
test-data.json:
If you see "TEST_NAME" in the output, you know the placeholder works!
Test Edge Cases¶
Always test with:
1. Empty arrays:
2. Single-item arrays:
3. Many items:
4. Minimum/maximum values:
5. Missing optional fields:
Validate JSON Before Testing¶
Always validate your JSON before using it with Templify:
- Go to jsonlint.com
- Paste your JSON
- Click "Validate JSON"
- Fix any errors
Common errors: - Missing or extra commas - Missing quotes - Unclosed brackets/braces
Error Prevention¶
Double-Check Placeholder Names¶
JSON keys are case-sensitive:
- Template: {{CustomerName}}
- JSON: "CustomerName" ✅
- JSON: "customername" ❌
- JSON: "customer_name" ❌
Always match the exact case from your JSON data.
Verify Closing Tags¶
Every opening tag needs a closing tag:
❌ Missing closing tag:
✅ Proper closing:
Match Brackets and Braces¶
❌ Wrong:
✅ Correct:
Avoid Spaces in Placeholders¶
❌ Wrong:
✅ Correct:
Performance Tips¶
Keep Templates Reasonably Sized¶
- Templates under 50 pages process quickly
- Templates 50-200 pages may take a few seconds
- Templates over 200 pages may be slow
Consider splitting very large documents into multiple templates.
Limit Loop Nesting¶
✅ Good (2 levels):
⚠️ Avoid (4+ levels):
{{#foreach A}}
{{#foreach B}}
{{#foreach C}}
{{#foreach D}}
... (hard to read and maintain)
{{/foreach}}
{{/foreach}}
{{/foreach}}
{{/foreach}}
Minimize Conditional Complexity¶
❌ Complex:
{{#if (Status = "Active" or Status = "Trial") and (Age >= 18 or HasParentConsent) and not (IsBanned or IsExpired) and AccountType = "Premium"}}
✅ Simpler (break into steps):
{{#if Status = "Active" or Status = "Trial"}}
{{#if Age >= 18 or HasParentConsent}}
{{#if not IsBanned and not IsExpired}}
{{#if AccountType = "Premium"}}
...
{{/if}}
{{/if}}
{{/if}}
{{/if}}
Formatting Best Practices¶
Use Format Specifiers¶
❌ Raw values:
Active: {{IsActive}} → Active: true
Price: {{Price}} → Price: 1234.5
Date: {{Date}} → Date: 2024-01-15T00:00:00
✅ Formatted:
Active: {{IsActive:yesno}} → Active: Yes
Price: {{Price:currency}} → Price: $1,234.50
Date: {{Date:date:MMM d}} → Date: Jan 15
Preserve Template Formatting¶
Templify preserves the formatting of your template text:
- Bold text in template stays bold in output
- Font family, size, color are preserved
- Paragraph alignment is maintained
- List formatting (bullets, numbering) is kept
Tip: Format your placeholders in Word to get the formatting you want in the output.
Use Markdown for Dynamic Formatting¶
When formatting needs to come from data:
JSON:
Template:
Output: "Welcome Alice, your account is active!" (with bold and italic applied)
Maintenance¶
Version Your Templates¶
Keep track of template versions:
File naming:
- invoice-template-v1.docx
- invoice-template-v2.docx
- invoice-template-2024-01-15.docx
Or add version in template:
Document Data Requirements¶
Create a companion document listing required data:
invoice-template-DATA.md:
# Invoice Template Data Requirements
## Required Fields
- InvoiceNumber (string)
- InvoiceDate (string, format: YYYY-MM-DD)
- Customer.Name (string)
- Customer.Address (string)
- LineItems (array of objects):
- Description (string)
- Quantity (number)
- UnitPrice (number)
- Total (number)
## Optional Fields
- Customer.TaxID (string)
- DiscountAmount (number)
- Notes (string)
Keep Examples Updated¶
Maintain a sample JSON file alongside your template:
Files:
- invoice-template.docx (the template)
- invoice-sample-data.json (sample data)
- invoice-output-example.docx (processed example)
Common Mistakes to Avoid¶
1. Forgetting {{/if}} or {{/foreach}}¶
Error: Template doesn't process correctly
Solution: Count your opening and closing tags:
2. Wrong Array Access¶
JSON:
❌ Wrong:
✅ Correct:
3. Comparing Wrong Types¶
JSON:
❌ Might not work:
✅ Better:
4. Case Mismatches¶
JSON:
❌ Won't match:
✅ Must match exactly:
5. Trailing Commas in JSON¶
❌ Invalid JSON:
✅ Valid JSON:
Troubleshooting Checklist¶
When things don't work:
- [ ] Validate JSON at jsonlint.com
- [ ] Check placeholder spelling matches JSON exactly
- [ ] Verify {{...}} has two braces on each side
- [ ] Confirm all {{#if}} have matching {{/if}}
- [ ] Confirm all {{#foreach}} have matching {{/foreach}}
- [ ] Check for spaces inside braces:
{{Name}}not{{ Name }} - [ ] Verify array access uses [index] not dot notation
- [ ] Test with simple data first
- [ ] Check that JSON types match expectations (numbers not strings)
Quick Reference¶
DO:¶
✅ Use descriptive variable names ✅ Validate JSON before testing ✅ Test with edge cases ✅ Comment complex logic in Word ✅ Group related data in JSON ✅ Use format specifiers ✅ Keep templates organized
DON'T:¶
❌ Use single-letter variable names ❌ Skip JSON validation ❌ Only test happy path ❌ Create overly complex conditionals ❌ Use inconsistent naming styles ❌ Forget closing tags ❌ Mix up case in placeholder names
Resources¶
- Template Syntax Reference - Complete syntax guide
- JSON Basics - Understanding JSON
- Examples Gallery - Real-world examples
- Placeholders Guide - Using placeholders effectively
- Conditionals Guide - If/else best practices
- Loops Guide - Working with arrays
Remember: Good templates are readable, maintainable, and well-tested. Take time to structure your data properly and document your work – your future self (and others) will thank you!