Overview
Conditionals allow you to include or exclude content based on data conditions. TemplateMark supports conditional rendering through the optional block and conditional expressions.
Optional Block
The {{#optional}} block renders content only when an optional field has a value:
{{#optional fieldName}}
Content to show when field exists
{{else}}
Content to show when field is missing
{{/optional}}
Basic Example
Data (with value)
Data (without value)
Template
Output (with value)
Output (without value)
Accessing Optional Properties
Within the optional block, you can access nested properties:
{{#optional loyaltyStatus}}
Your loyalty level: {{level}}
{{else}}
No loyalty status found.
{{/optional}}
Clause with Condition
The {{#clause}} block can include a condition attribute for conditional rendering:
{{#clause object condition="return expression"}}
Content to render if condition is true
{{/clause}}
Conditional Clause Example
Data (without address)
Data (with address)
Template
Output (without address)
Output (with address)
Condition Expressions
Condition expressions are JavaScript expressions that return a boolean value.
Syntax
condition="return EXPRESSION"
The condition must start with return and use valid JavaScript syntax.
Common Condition Patterns
Check Existence
Compare Values
Multiple Conditions
Type Checks
condition="return value !== undefined"
condition="return object !== null"
condition="return amount > 100"
condition="return status === 'active'"
condition="return count >= 5"
condition="return amount > 0 && currency === 'USD'"
condition="return status === 'active' || status === 'pending'"
condition="return typeof value === 'string'"
condition="return Array.isArray(items)"
condition="return items.length > 0"
Combining Conditionals
You can nest conditional blocks to create complex logic:
{{#optional customer}}
Customer: {{name}}
{{#clause address condition="return address !== undefined"}}
Address:
{{line1}}, {{city}}
{{/clause}}
{{else}}
No customer information available.
{{/optional}}
Use Cases
Optional customer details
Show additional information only when available: ### Customer Profile
Name: {{name}}
{{#optional phoneNumber}}
Phone: {{phoneNumber}}
{{/optional}}
{{#optional email}}
Email: {{email}}
{{/optional}}
Conditional invoice sections
Include sections based on invoice type: {{#clause discountInfo condition="return discount > 0"}}
### Discount Applied
You saved {{discount as "0,0.00"}}!
{{/clause}}
Show different terms based on contract type: {{#clause termsOfService condition="return userType === 'business'"}}
## Business Terms
{{businessTerms}}
{{/clause}}
{{#clause termsOfService condition="return userType === 'individual'"}}
## Individual Terms
{{individualTerms}}
{{/clause}}
Only show sensitive information when authorized: {{#clause confidentialInfo condition="return authorized === true"}}
## Confidential Information
{{sensitiveDetails}}
{{/clause}}
Best Practices
Condition expressions must be valid JavaScript Ensure your conditions use proper JavaScript syntax and always start with return: Good: condition="return amount > 0"
Bad: condition="amount > 0"
Prefer optional blocks for simple existence checks Use {{#optional}} instead of clause conditions when only checking if a value exists: Preferred:
{{#optional address}}
{{line1}}
{{/optional}}
Also works:
{{#clause address condition="return address !== undefined"}}
{{line1}}
{{/clause}}
Test both conditional branches Always test your templates with data that triggers both the true and false conditions to ensure correct behavior.
Variables Learn about variable syntax and formatting
Formulas Use JavaScript expressions for complex logic
Lists Conditionally render list items