Skip to main content

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

{
  "$class": "[email protected]",
  "loyaltyStatus": {
    "$class": "[email protected]",
    "level": "Gold"
  }
}

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

{
  "$class": "[email protected]"
}

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

condition="return value !== undefined"
condition="return object !== null"

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

Show additional information only when available:
### Customer Profile
Name: {{name}}

{{#optional phoneNumber}}
Phone: {{phoneNumber}}
{{/optional}}

{{#optional email}}
Email: {{email}}
{{/optional}}
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 JavaScriptEnsure 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 checksUse {{#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 branchesAlways 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

Build docs developers (and LLMs) love