Skip to main content

Using Handlebars Conditionals in Email Templates

Handlebars conditionals allow you to show or hide content in your email templates based on specific conditions. This guide covers how to use {{#if}} and {{#unless}} statements to create dynamic, personalized emails.

Basic If Statements

The {{#if}} helper allows you to conditionally display content when a value exists or is truthy.

Syntax

{{#if variable}}
This content shows when variable exists and is truthy
{{/if}}

Example: Show Content Based on Reservation Type

{{#if reservation.reservation_type}}
Reservation Type: {{reservation.reservation_type}}

**Total Price**: {{format-currency reservation.total_revenue "$"}}
**Amount Paid**: {{format-currency reservation.total_paid "$"}}
**Balance:** {{format-currency (basic-math reservation.total_revenue "-" reservation.total_paid) "$"}}
{{/if}}

Unless Statements

The {{#unless}} helper is the opposite of {{#if}} - it shows content when a condition is false or when a value doesn't exist.

Syntax

{{#unless variable}}
This content shows when variable is false or doesn't exist
{{/unless}}

Example: Hide Content for Specific Reservation Types

{{#unless (eq reservation.reservation_type 'OWN')}}
{{reservation.reservation_type}} Below is your receipt and transaction information:

**Total Price**: {{format-currency reservation.total_revenue "$"}}
**Amount Paid**: {{format-currency reservation.total_paid "$"}}
**Balance:** {{format-currency (basic-math reservation.total_revenue "-" reservation.total_paid) "$"}}
{{/unless}}

This example shows the receipt information for all reservation types except 'OWN'.

If-Else Statements

You can use {{else}} to provide alternative content when a condition is not met.

Syntax

{{#if condition}}
Content when condition is true
{{else}}
Content when condition is false
{{/if}}

Example: Different Messages Based on Payment Status

{{#if reservation.total_paid}}
Thank you for your payment of {{format-currency reservation.total_paid "$"}}
{{else}}
No payment has been received yet
{{/if}}

Nested Conditionals

You can nest conditional statements to create more complex logic.

Example: Multiple Condition Checks

{{#if reservation.reservation_type}}
{{#if reservation.total_revenue}}
Reservation Type: {{reservation.reservation_type}}
Total: {{format-currency reservation.total_revenue "$"}}

{{#if reservation.total_paid}}
Payment Received: {{format-currency reservation.total_paid "$"}}
{{else}}
No payment received yet
{{/if}}
{{else}}
Reservation type exists but no pricing information available
{{/if}}
{{else}}
No reservation information available
{{/if}}

Common Use Cases

1. Personalized Greetings

{{#if contact.first_name}}
Hello {{contact.first_name}},
{{else}}
Hello,
{{/if}}

2. Conditional Content Based on Guest History

{{#if contact.total_reservations}}
{{#if (gt contact.total_reservations 5)}}
As a frequent guest, you've earned VIP status!
{{else}}
Thank you for being a returning guest!
{{/if}}
{{else}}
Welcome! We're excited to host you for the first time.
{{/if}}

{{#if reservation.has_future}}
We look forward to your upcoming reservations!
{{/if}}

3. Dynamic Reservation Details

{{#if reservation.deposit}}
**Deposit Required: {{format-currency reservation.deposit "$"}}**
{{#if reservation.total_paid}}
✓ Deposit paid
{{else}}
⚠ Deposit pending
{{/if}}
{{/if}}

{{#if reservation.cancelled_at}}
**This reservation was cancelled on {{format-date reservation.cancelled_at "MMM DD, YYYY"}}**
{{else}}
**Status: {{reservation.status}}**
{{/if}}

4. Conditional Unit Information

{{#if units.0.wifi_ssid}}
## WiFi Information
Network: {{units.0.wifi_ssid}}
Password: {{units.0.wifi_code}}
{{/if}}

{{#if units.0.door_code}}
## Access Codes
Door Code: {{units.0.door_code}}
{{#if units.0.gate_code}}
Gate Code: {{units.0.gate_code}}
{{/if}}
{{/if}}

5. Contact Communication Preferences

{{#unless contact.dont_send_email}}
<p>You're receiving this email because you've opted in to our communications.</p>
{{/unless}}

{{#if contact.birthday}}
{{#if (eq (format-date contact.birthday "MM") (format-date "now" "MM"))}}
🎉 Happy Birthday Month! Enjoy a special discount on your next stay.
{{/if}}
{{/if}}

Best Practices

  1. Keep Logic Simple: Complex conditional logic can be hard to maintain. Consider simplifying your conditions or handling complex logic before the email is sent.

  2. Test Thoroughly: Always test your templates with different data scenarios to ensure conditionals work as expected.

  3. Provide Fallbacks: Use {{else}} blocks to provide default content when conditions aren't met.

  4. Check for Empty Values: Remember that empty strings, zero, and false are considered falsy in Handlebars.

  5. Use Descriptive Variable Names: Make your templates easier to understand by using clear, descriptive variable names.

  6. Handle Date Comparisons: When comparing dates, use format helpers to ensure consistent comparisons.

  7. Consider Locale: For international guests, use {{contact.locale}} to conditionally show content in different languages.

Limitations

  • Handlebars conditionals only support simple boolean logic
  • You cannot use complex expressions directly in {{#if}} statements without helpers
  • For equality checks and other comparisons, you'll need to use helpers like (eq) as shown in the examples