What is Handlebars and How to Use It in MSG91?

#


Personalize Emails with Handlebars


Handlebars is a templating language that lets you create dynamic email content using expressions inside double curly braces {{ }}.

In MSG91, you can use Handlebars to:

  • Insert variables like customer name, order ID, or any other data.

  • Show or hide blocks of content based on simple conditions.

  • Loop through lists such as items in an order.

This document shows what is supported in MSG91тАЩs Handlebars implementation and how to use it safely when creating templates.


#

1. What Handlebars features are available in MSG91?

MSG91 supports the core Handlebars helpers:

#

Variables

#

Conditions (if / else)

  • {{#if isPremium}} тАж {{/if}}

  • {{#if isPremium}} тАж {{else}} тАж {{/if}}

#

Negative condition (unless)

  • {{#unless hasOrdered}} тАж {{/unless}}

#

Loops (each)

  • {{#each order.items}} тАж {{/each}}

#

Root context (advanced use)

  • {{@root.supportPhone}} тАФ access a top-level variable from inside a nested block.


#

1.1 Helpers that are NOT available

These helpers are not supported in MSG91 and will cause errors such as тАЬMissing helperтАЭ:

  • Logical helpers: and, or, {{#and}}, {{#or}}

  • Comparison helpers: greaterThan, lessThan, gt, lt, equals, notEquals

  • Utility helpers: length, subtract, and any other custom helper not added by MSG91

Important:
To ensure your templates work, use only:

  • if

  • else

  • unless

  • each

And send simple boolean flags from your backend.


#

2. Common patterns for building templates

These reusable patterns work with MSG91тАЩs supported Handlebars features.


#

2.1 Simple show/hide with if and else

Use this when you want different text for different users.

Template

{{#if isPremium}}
  <p>Thanks for being a premium user! Enjoy your exclusive benefits.</p>
{{else}}
  <p>Upgrade to premium to unlock more benefits.</p>
{{/if}}

Sample data

{
  "isPremium": true
}

#

2.2 Negative condition using unless

Use this when you want to show a reminder only if something has not happened.

Template

{{#unless hasOrdered}}
  <p>You havenтАЩt placed an order yet. Use code WELCOME10 to get 10% off!</p>
{{/unless}}

Sample data

{
  "hasOrdered": false
}

#

2.3 тАЬANDтАЭ condition (A and B) using nested if

Handlebars does not support тАЬandтАЭ, so we use nested conditions.

Template

<p>
Hello {{name}}!
{{#if hasFoodPreference}}
  {{#if hasDrinkPreference}}
    Thank you for letting us know your dining preferences.
  {{/if}}
{{/if}}
 We look forward to sending you more delicious recipes.
</p>

Sample data

{
  "name": "Ben",
  "hasFoodPreference": true,
  "hasDrinkPreference": true
}

#

2.4 тАЬORтАЭ condition (A or B) using a boolean flag

Compute OR in your application.

Template

<p>
Hello {{name}}!
{{#if hasOutdoorInterest}}
  We think you might enjoy a map of trails in your area.
{{else}}
  WeтАЩd love to know more about the outdoor activities you enjoy.
{{/if}}
 Have a great day.
</p>

Sample data

{
  "name": "Ben",
  "isRunner": true,
  "isCyclist": false,
  "hasOutdoorInterest": true
}

#

2.5 Comparison via boolean flags

For example: тАЬbalance above тВ╣1,00,000тАЭ or тАЬitems in cartтАЭ.

High balance example

{{#if user.isHighBalance}}
  <p>Alert: Your account balance exceeds тВ╣1,00,000.</p>
{{/if}}

Sample data

{
  "user": {
    "balance": 125000,
    "isHighBalance": true
  }
}

Cart example

<p>
Hello {{name}}!
{{#if hasCartItems}}
  You have items left in your cart. Continue checkout anytime.
{{else}}
  Thanks for browsing our site.
{{/if}}
</p>

Sample data

{
  "name": "Ben",
  "hasCartItems": true
}

#

2.6 Looping with each

Template

<ol>
  {{#each user.orderHistory}}
    <li>You ordered: {{this.item}} on {{this.date}}</li>
  {{/each}}
</ol>

Sample data

{
  "user": {
    "orderHistory": [
      { "date": "2025-02-01", "item": "NIKE Air Force" },
      { "date": "2025-05-01", "item": "Air Jordans" },
      { "date": "2025-07-01", "item": "Converse All Stars" }
    ]
  }
}

#

3. Quick cheat sheet

Need

Template pattern

Data you must send

Show content for premium users

{{#if isPremium}} тАж {{/if}}

"isPremium": true/false

Reminder if no order

{{#unless hasOrdered}} тАж {{/unless}}

"hasOrdered": true/false

A AND B

Nested {{#if}}

Two booleans

A OR B

Single {{#if flag}}

One OR-boolean

Compare threshold

{{#if someFlag}}

Precomputed boolean

List items

{{#each items}}

Array

Access root variable

{{@root.xyz}}

Root variable

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>MSG91 Handlebars Combined Test</title>
  </head>
  <body>

    <!-- 2.1 Simple show/hide with if / else -->
    <h2>Premium Section</h2>
    {{#if isPremium}}
      <p>Thanks for being a premium user! Enjoy your exclusive benefits.</p>
    {{else}}
      <p>Upgrade to premium to unlock more benefits.</p>
    {{/if}}

    <hr />

    <!-- 2.2 Negative condition with unless -->
    <h2>Order Reminder</h2>
    {{#unless hasOrdered}}
      <p>You havenтАЩt placed an order yet. Use code <strong>WELCOME10</strong> to get 10% off your first purchase!</p>
    {{/unless}}

    <hr />

    <!-- 2.3 AND condition with nested if -->
    <h2>Dining Preferences (AND condition)</h2>
    <p>
      Hello {{name}}!
      {{#if hasFoodPreference}}
        {{#if hasDrinkPreference}}
          Thank you for letting us know your dining preferences.
        {{/if}}
      {{/if}}
      We look forward to sending you more delicious recipes.
    </p>

    <hr />

    <!-- 2.4 OR condition with boolean flag -->
    <h2>Outdoor Interest (OR flag)</h2>
    <p>
      Hello {{name}}!
      {{#if hasOutdoorInterest}}
        We think you might enjoy a map of trails in your area.
      {{else}}
        WeтАЩd love to know more about the outdoor activities you enjoy.
      {{/if}}
      Have a great day.
    </p>

    <hr />

    <!-- 2.5 Comparison via boolean flags: high balance -->
    <h2>High Balance Alert</h2>
    {{#if user.isHighBalance}}
      <p>Alert: Your account balance exceeds тВ╣1,00,000. Consider investing to grow your savings.</p>
    {{/if}}

    <!-- 2.5 Comparison via boolean flags: cart items -->
    <h2>Cart Reminder</h2>
    <p>
      Hello {{name}}!
      {{#if hasCartItems}}
        It looks like you still have some items in your shopping cart.
        Sign back in to continue checking out at any time.
      {{else}}
        Thanks for browsing our site. We hope you'll come back soon.
      {{/if}}
    </p>

    <hr />

    <!-- 2.6 Looping over lists with each -->
    <h2>Order History</h2>
    <ol>
      {{#each user.orderHistory}}
        <li>You ordered: {{this.item}} on: {{this.date}}</li>
      {{/each}}
    </ol>

    <hr />

    <!-- Cheat sheet: generic items list -->
    <h2>Generic Items List (cheat sheet pattern)</h2>
    <ul>
      {{#each items}}
        <li>{{this}}</li>
      {{/each}}
    </ul>

    <!-- Cheat sheet: root context example -->
    <p>
      For help, contact us at: {{@root.supportPhone}}
    </p>

  </body>
</html>
{
  "isPremium": true,
  "hasOrdered": false,

  "name": "Bhaskar",

  "hasFoodPreference": true,
  "hasDrinkPreference": true,

  "isRunner": true,
  "isCyclist": false,
  "hasOutdoorInterest": true,

  "hasCartItems": true,
  "cartItems": [
    "raft",
    "water bottle",
    "sleeping bag"
  ],

  "user": {
    "balance": 125000,
    "isHighBalance": true,
    "orderHistory": [
      { "date": "2025-02-01", "item": "NIKE Air Force" },
      { "date": "2025-05-01", "item": "Air Jordans" },
      { "date": "2025-07-01", "item": "Converse All Stars" }
    ]
  },

  "items": [
    "Item A",
    "Item B",
    "Item C"
  ],

  "supportPhone": "+91-9876543210"
}
#

4. DoтАЩs and DonтАЩts

#

Do:

  • Keep logic simple (if, else, unless, each)

  • Compute complex logic in backend

  • Test with sample JSON

  • Use booleans like isHighBalance, hasItems, etc.

#

DonтАЩt:

  • DonтАЩt use unsupported helpers (or, and, greaterThan, equals, subtract, etc.)

  • DonтАЩt put business logic inside templates

  • DonтАЩt reference variables you donтАЩt send


#

5. Why Use Handlebars in MSG91?

  • Multiple emails flying everywhere тАФ hard to track
    тЖТ Always accurate and personalized using live data.

  • Credits wasted for separate email content
    тЖТ Save credits тАФ one dynamic template handles all orders.

  • Templates too rigid
    тЖТ Handlebars enables dynamic emails at scale.

Compatible with automated workflows and transactional emails.


#

6. How to Create a Template in MSG91

#

1. Log in to your MSG91 Email account

Open your MSG91 dashboard.

#

2. Go to Templates тЖТ Create Template

Navigate to Email тЖТ Templates тЖТ Create Template.

#

3. Choose тАЬStart from ScratchтАЭ or Use AI

  • Build a template manually

  • Or generate via AI, then enhance with Handlebars

#

4. Insert Handlebars in the Email Body

Use Handlebars directly in your HTML editor to show dynamic content.

#

5. Test Your Template

Use Preview or provide sample JSON.

#

6. Submit and Launch

Submit for approval тЖТ Once approved, use in campaigns or workflows.


#

7. Need Help?

If youтАЩre unsure about your data or template logic, weтАЩre here to help.

Email: support@msg91.com
Live Chat: Available in MSG91 Dashboard