Skip to navigation

Handlebars is a JavaScript library used for creating dynamic templates.

It allows you to define templates with placeholders, and these placeholders are replaced with data when the template is triggered with the information sent in the payload.

Handlebars within Fyno support features like conditional operations, loops, date time formatting, partial searches, and custom helpers, making it a powerful tool for building your templates.

Handlebars in Fyno’s Templates

To understand how handlebars work, we first need to establish a sample payload, based off which the Handlebars function and applicability can be demonstrated.

Sample Payload
{
"members": [
{
"first_name": "Ashwin",
"last_name": "Agarwal",
"city": ""
}
],
"investments": [
{
"amount": 10,
"company": "L&T"
},
{
"amount": 20
}
],
"companies": {
"L&T": "India",
"Apple": "USA"
},
"user_type": "pro",
"period": {
"start": "2023-10-01",
"end": "2023-10-03"
},
"classification": ["active", "pro"]
}

Fyno has default as well as custom handlebars that you can use in your templates. Let’s have a look at what these are and how to use them using the above payload and data as example responses. You can also generate handlebars with Fyno AI by describing what you need in plain language.

Use single quotes inside handlebars

Double quotes inside handlebars can break your template syntax, especially in emails. Use single quotes instead. For example, use {{math number1 '+' number2}} instead of {{math number1 "+" number2}}.

Format
{{key}}
Example
{{user_type}}
Output
pro
Format
{{key.subkey}}
Example
{{period.start}} to {{period.end}}
Output
2023-10-01 to 2023-10-03
Format
{{key.index.subkey}}
Example
{{members.0.first_name}}
Output
Ashwin
Format
{{key.index}}
Example
{{classification.0}}
Output
active
Format
{{{key}}}
Example
{{{investments.0.company}}}
If you use double braces instead of triple, L&T shows as L&T.
Output
L&T
Format
{{key.length}}
Example
User made {{investments.length}} investments
Output
User made 2 investments
Format
{{#if variable}}
Content Here
{{else if variable}}
Content Here
{{else}}
Content Here
{{/if}}
Example
{{#if members.0.city}}
City available for member 1 and may be member 2
{{else if members.1.city}}
City available for member 2 only
{{else}}
City not available for member 1 and 2
{{/if}}
Output
City available for member 1 and may be member 2
Format
{{#if (eq variable 'value')}}
Content Here
{{else if (eq variable 'value')}}
Content Here
{{/if}}
Example
{{#if (eq investments.0.company 'L&T')}}
First investment was L&T
{{else if (eq investments.0.company 'Apple')}}
First investment was Apple
{{else}}
First investment was neither Apple nor L&T
{{/if}}
Output
First investment was L&T
Format
{{#ifEquals variable 'value'}}
Content Here
{{else ifEquals variable 'value'}}
Content Here
{{else}}
Content Here
{{/ifEquals}}
Example
{{#ifEquals investments.0.company 'L&T'}}
First investment was L&T
{{else ifEquals investments.0.company 'Apple'}}
First investment was Apple
{{else}}
First investment was neither Apple nor L&T
{{/ifEquals}}
Output
First investment was L&T
Format
{{#if (and/or (eq/ne/lt/gt/lte/gte variable 'value') (eq/ne/lt/gt/lte/gte variable 'value'))}}
Content Here
{{else}}
Content Here
{{/if}}
Example
{{#if (or (eq investments.0.company 'L&T') (eq investments.0.company 'Apple'))}}
First investment is either L&T OR Apple
{{/if}}
{{#if (and (eq investments.0.company 'L&T') (gt investments.0.amount 5))}}
First investment is L&T and investment amount is greater than 5
{{/if}}
{{#if (and (ne user_type 'pro') (gte investments.0.amount 10))}}
User is not pro but first investment is greater than or equal to 10
{{else if (and (eq user_type 'pro') (lt investments.0.amount 10))}}
User type is pro but first investment is less than 10
{{else if (and (eq user_type 'pro') (eq investments.0.amount 10))}}
User type is pro and first investment is equal to 10
{{/if}}
Output
First investment is either L&T OR Apple
First investment is L&T and investment amount is greater than 5
User type is pro and first investment is equal to 10
Format
{{#ifMatches variable 'regex'}}
Content Here
{{else ifMatches variable 'regex'}}
Content Here
{{else}}
Content Here
{{/ifMatches}}
Example
{{#ifMatches members.0.last_name 'Aga\*'}}
Matched
{{else}}
Not matched
{{/ifMatches}}
Output
Matched
Format
{{#ifStartsWith variable 'value'}}
Content Here
{{else ifStartsWith variable 'value'}}
Content Here
{{else}}
Content Here
{{/ifStartsWith}}
Example
{{#ifStartsWith members.0.first_name 'A'}}
Name starts with A
{{else}}
Name does not start with A
{{/ifStartsWith}}
Output
Name starts with A
Format
{{#compare var1 'or' var2}}
Content Here
{{else compare var1 'or' var2}}
Content Here
{{/compare}}
Example
{{#compare investments.0.amount 'or' investments.1.amount}}
Invested at least once
{{else}}
No investment made
{{/compare}}
Output
Invested at least once
Format
{{#compare var1 'and' var2}}
Content Here
{{else compare var1 'and' var2}}
Content Here
{{/compare}}
Example
{{#compare investments.0.amount 'and' investments.1.amount}}
Invested at least twice
{{else}}
One or fewer investments made
{{/compare}}
Output
Invested at least twice
Format
{{default variable 'value'}}
Example
{{default members.0.city 'Bangalore'}}
Output
Bangalore
Format
{{math number1 '+' number2}}
Example
{{math investments.0.amount '+' investments.1.amount}}
Output
30
Format
{{math number1 '-' number2}}
Example
{{math investments.1.amount '-' investments.0.amount}}
Output
10
Format
{{math number1 '*' number2}}
Example
{{math investments.0.amount '*' investments.1.amount}}
Output
200
Format
{{math number1 '/' number2}}
Example
{{math investments.0.amount '/' investments.1.amount}}
Output
0.5
Format
{{math number1 '%' number2}}
Example
{{math investments.0.amount '%' investments.1.amount}}
Output
10
Format
{{math number 'round' decimal_places}}
Example
{{math investments.0.amount 'round' 2}}
{{math investments.0.amount 'round'}}
Output
10.00
10
Format
{{#unless key}}
Content Here
{{/unless}}
Example
{{#unless investments}}
Investments were not made!
{{else}}
Investments were made!
{{/unless}}
Output
Investments were made!
Format
{{#each key}}
{{this.subkey}}
{{/each}}
Example
{{#each investments}}
{{math @index '+' 1}}.
{{{default this.company 'Not known'}}}
- ${{this.amount}}
{{else}}
No investments were made!
{{/each}}
Output
1. L&T - $10
2. Not known - $20
Format
{{#if variable}}
Content Here
{{else}}
{{#ifEquals variable 'value'}}
Content Here
{{else}}
{{#ifMatches variable 'regex'}}
Content Here
{{else}}
{{#ifStartsWith variable 'value'}}
Content Here
{{else}}
{{#if (eq var1 var2)}}
Content Here
{{/if}}
{{/ifStartsWith}}
{{/ifMatches}}
{{/ifEquals}}
{{/if}}
Example
{{#if investments}}
{{#ifEquals investments.0.company 'Apple'}}
Invested in Apple Inc!
{{else}}{{#ifMatches investments.0.company 'Amazon\*'}}
Invested in Amazon ventures!
{{else}}{{#ifStartsWith investments.0.company 'Microsoft'}}
Invested in Microsoft!
{{else}}{{#if (eq investments.0.company 'L&T')}}
Invested in Larsen & Toubro!
{{/if}}
{{/ifStartsWith}}
{{/ifMatches}}
{{/ifEquals}}
{{else}}
No investments were made!
{{/if}}
Output
Invested in Larsen & Toubro!
Format
{{#each iterate_key}}
{{subkey}}
{{lookup ../key [subkey]}}
{{/each}}
Example
{{#each investments as | investment |}}
{{#if company}}
{{{company}}} is in {{lookup ../companies [company]}}
{{/if}}
{{/each}}
Output
L&T is in India
Format
{{split variable_name 'delimiter' index}}
Example
{{split period.start '-' -1}}/{{split period.start '-' 1}}/{{split period.start '-' 0}} {{split period.end '-' -1}}/{{split period.end '-' 1}}/{{split period.end '-' 0}}
Output
01/10/2023
03/10/2023
Format
{{#each (split variable_name 'delimiter')}}
{{this}}
{{/each}}
Example
{{#each (split period.start '-')}}
{{this}}
{{/each}}
Output
2023
10
01
Format
{{relativeDay timestamp_with_timezone}}
Example
{{relativeDay '2023-10-25T14:30:00+05:30'}}
{{relativeDay '2023-10-26T14:30:00+05:30'}}
{{relativeDay '2023-10-24T14:30:00+05:30'}}
{{relativeDay period.start}}
{{#ifEquals (relativeDay '2023-12-19T14:30:00+05:30') 'today'}}
The date is today!
{{else}}
The date is not today!
{{/ifEquals}}
Output
today
tomorrow
yesterday
before
The date is today!
Format
{{dateDiff timestamp1_with_timezone timestamp2_with_timezone 'ms / seconds / minutes / hours / days / weeks / months / years'}}
The date and time must be in RFC 3339 or ISO 8601 format, or the helper returns NaN.
Example
{{dateDiff '2023-12-01T14:30:00+05:30' '2023-12-19T14:30:00+05:30' 'ms'}}
ms
{{dateDiff '2023-12-01T14:30:00+05:30' '2023-12-19T14:30:00+05:30' 'seconds'}}
secs
{{dateDiff '2023-12-01T14:30:00+05:30' '2023-12-19T14:30:00+05:30' 'minutes'}}
mins
{{dateDiff '2023-12-01T14:30:00+05:30' '2023-12-19T14:30:00+05:30' 'hours'}}
hours
{{dateDiff '2023-12-01T14:30:00+05:30' '2023-12-19T14:30:00+05:30' 'days'}}
days
{{dateDiff '2023-12-01T14:30:00+05:30' '2023-12-19T14:30:00+05:30' 'weeks'}}
weeks
{{dateDiff '2023-12-01T14:30:00+05:30' '2023-12-19T14:30:00+05:30' 'months'}}
months
{{dateDiff '2023-12-01T14:30:00+05:30' '2023-12-19T14:30:00+05:30' 'year'}}
years Since
{{dateDiff '2023-12-01T14:30:00+05:30' 'NOW' 'days'}}
days
{{#if (gt (dateDiff '2023-12-01T14:30:00+05:30' 'NOW' 'days') 10)}}
More than 10 days!
{{else}}
Less than 10 days!
{{/if}}
Output
1555200000 ms
1555200 secs
25920 mins
432 hours
18 days
2 weeks
0 months
0 years
Since 18 days
More than 10 days!
Format
{{formatDate timestamp_with_timezone 'short'}}
{{formatDate timestamp_with_timezone 'short' 'en-in'}}
{{formatDate timestamp_with_timezone 'short' 'en-in' '+05:30'}}
{{formatDate timestamp_with_timezone 'medium'}}
{{formatDate timestamp_with_timezone 'medium' 'en-in'}}
{{formatDate timestamp_with_timezone 'medium' 'en-in' '+05:30'}}
{{formatDate timestamp_with_timezone 'long'}}
{{formatDate timestamp_with_timezone 'long' 'en-in'}}
{{formatDate timestamp_with_timezone 'long' 'en-in' '+05:30'}}
Example
{{formatDate '2023-10-25T18:30:00Z' 'short'}}
{{formatDate '2023-10-25T18:30:00Z' 'short' 'en-in'}}
{{formatDate '2023-10-25T18:30:00Z' 'short' 'en-in' '+05:30'}}
{{formatDate '2023-10-25T18:30:00Z' 'medium'}}
{{formatDate '2023-10-25T18:30:00Z' 'medium' 'en-in'}}
{{formatDate '2023-10-25T18:30:00Z' 'medium' 'en-in' '+05:30'}}
{{formatDate '2023-10-25T18:30:00Z' 'long'}}
{{formatDate '2023-10-25T18:30:00Z' 'long' 'en-in'}}
{{formatDate '2023-10-25T18:30:00Z' 'long' 'en-in' '+05:30'}}
Output
10/25/2023
25/10/2023
26/10/2023
Oct 25, 2023
25 Oct 2023
26 Oct 2023
October 25, 2023
25 October 2023
26 October 2023
Format
{{formatDateTime timestamp_with_timezone 'short'}}
{{formatDateTime timestamp_with_timezone 'short' 'en-in'}}
{{formatDateTime timestamp_with_timezone 'short' 'en-in' '+05:30'}}
{{formatDateTime timestamp_with_timezone 'medium'}}
{{formatDateTime timestamp_with_timezone 'medium' 'en-in'}}
{{formatDateTime timestamp_with_timezone 'medium' 'en-in' '+05:30'}}
{{formatDateTime timestamp_with_timezone 'long'}}
{{formatDateTime timestamp_with_timezone 'long' 'en-in'}}
{{formatDateTime timestamp_with_timezone 'long' 'en-in' '+05:30'}}
{{formatDateTime timestamp_with_timezone 'custom' 'en-in' '+05:30'}}
Example
{{formatDateTime '2023-10-25T18:30:00Z' 'short'}}
{{formatDateTime '2023-10-25T18:30:00Z' 'short' 'en-in'}}
{{formatDateTime '2023-10-25T18:30:00Z' 'short' 'en-in' '+05:30'}}
{{formatDateTime '2023-10-25T18:30:00Z' 'medium'}}
{{formatDateTime '2023-10-25T18:30:00Z' 'medium' 'en-in'}}
{{formatDateTime '2023-10-25T18:30:00Z' 'medium' 'en-in' '+05:30'}}
{{formatDateTime '2023-10-25T18:30:00Z' 'long'}}
{{formatDateTime '2023-10-25T18:30:00Z' 'long' 'en-in'}}
{{formatDateTime '2023-10-25T18:30:00Z' 'long' 'en-in' '+05:30'}}
{{formatDateTime '2023-10-25T18:30:00Z' 'custom' 'en-in' '+05:30'}}
Output
25/10/2023, 18:30
25/10/2023, 06:30 pm
26/10/2023, 12:00 am
25 Oct 2023, 18:30
25 Oct 2023, 06:30 pm
26 Oct 2023, 12:00 am
25 October 2023 at 18:30
25 October 2023 at 06:30 pm
26 October 2023 at 12:00 am
26 October 2023 at 12:00:00 am
Format
{{formatNumber number}}
{{formatNumber number 'en-in'}}
{{formatNumber (math number 'round') 'en-in'}}
Example
{{formatNumber 4000000.112}}
{{formatNumber 4000000.112 'en-in'}}
{{formatNumber (math 4000000.11 'round') 'en-in'}}
{{formatNumber 4000000.112 'en-in' 'notation=compact'}}
{{formatNumber 4000000.112 'en-US' 'notation=compact'}}
{{formatNumber 42456235.112 'en-US' 'notation=compact, minimumFractionDigits=2, maximumFractionDigits=2, style=currency, currency=USD'}}
{{formatNumber 42456235.112 'en-in' 'notation=compact, minimumFractionDigits=2, maximumFractionDigits=2, style=currency, currency=INR'}}
Output
4,000,000.112
40,00,000.112
40,00,000
40L
4M
$42.46M
₹4.25Cr
Format
{{trim variable_name}}
Example
{{trim ' Test Name '}}
Output
Test Name
Format
{{trim variable_name max_length}}
Example
{{trim 'First Middle Last Name' 20}}
Output
First Middle Last Na
Format
{{trim variable_name max_length delimiter}}
Example
{{trim 'First Middle Last Name' 20 ' '}}
Output
First Middle Last
Format
{{remove variable_name 'remove_char/string'}}
Example
{{remove 'Fyno™' '™'}}
{{remove 'Fyno®' '®'}}
{{remove 'Fyno®™' '®|™'}}
Output
Fyno
Fyno
Fyno
Format
{{sumAll start_key 'number_key'}}
Example
{{sumAll @root 'investments.amount'}}
{{formatNumber (sumAll @root 'investments.amount') 'en-US' 'style=currency, currency=USD'}}
Output
30
$30.00
Format
{{moment start_time format='date_format'}}
Example
{{moment start_time format='D/M/Y'}}
{{moment start_time format='D/M/Y hh:mm A'}}
{{moment start_time format='dddd, D MMMM'}}
{{moment start_time format='dddd, Do MMMM Y HH:mm Z'}}
{{moment start_time format='DD/MM/YYYY'}}
Output
26/11/2024
26/11/2024 02:51 PM
Tuesday, 26 November
Tuesday, 26th November 2024 14:54 +05:30
26/11/2024
Format
{{moment date format='date_format' tz='timezone'}}
Example
{{moment '1722605334190' format='D/M/Y'}}
{{moment '1722605334190' format='DD/MM/YYYY hh:mm A'}}
{{moment '1722605334190' format='dddd, D MMMM'}}
{{moment '1722605334190' format='dddd, Do MMMM Y HH:mm Z'}}
{{moment '1722605334190' format='dddd, Do MMMM Y HH:mm Z' tz='Europe/Andorra'}}
Output
2/8/2024
02/08/2024 06:58 PM
Friday, 2 August
Friday, 2nd August 2024 18:58 +05:30
Friday, 2nd August 2024 15:28 +02:00
Format
{{numberToWord number}}
Example
{{numberToWord 6}}
{{numberToWord 6231}}
{{numberToWord 100000000}}
Output
six
six thousand two hundred thirty one
one hundred million
Format
{{unidecode variable_name}}
Example
{{unidecode Hello® |®|™}}
{{unidecode Ryyúuy}}
{{unidecode नमस्ते, आप कैसे हैं?}}
Output
Hello(r) |(r)|tm
Ryyuuy
nmste, aap kaise haiN?

Generate Handlebars with Fyno AI

Don’t want to write the syntax yourself? Describe what you need in plain language, and Fyno AI will generate a ready-to-use handlebar for you.

Where to find it

You can open the generator from two places:

  • Left panel on the template page: Expand Handlebars > Build Your Own and click Generate. If you’d rather build the logic yourself, this section also links to the Handlebars Builder.
  • AI menu on input fields: Any field that supports handlebars has an AI menu. Choose Generate Handlebar from it.

The AI menu also has Improve Handlebars, which refines a handlebar that’s already in the field instead of starting from scratch.

Build Your Own section in the left panel, with the Generate button

Generate a handlebar

1

Describe your requirement

In the Generate Handlebars window, describe what the handlebar should do. For example: Show order_date in short Indian date format. Then click Generate.

Fyno AI works only with placeholders that already exist in your template. For example, to show a date in a specific format, the date must already be a placeholder in your template.

Generate Handlebars window with a requirement entered

2

Let Fyno AI work

While Brewing your handlebar is shown, Fyno AI reads your requirement, looks up the best-fitting helper from the handlebar library, and writes the handlebar, checking its syntax along the way. You can click Cancel at any point.

Fyno AI generating a handlebar

3

Review and copy

Fyno AI shows the generated handlebar with a sample output, so you can confirm it does what you expect.

Click Copy and paste it into your template.

Always test a generated handlebar with a sample payload before publishing your template, just as you would with one you wrote yourself.
Check the quotes in generated handlebars

Arguments inside a handlebar must use single quotes, because double quotes can break your template, especially in emails. If a generated handlebar contains double quotes, replace them with single quotes before you use it.

Example requirements

These examples use the sample payload from earlier on this page.

Requirement
Show members.0.city. If it's empty, show Bangalore instead.
Generated handlebar
{{default members.0.city 'Bangalore'}}
Output
Bangalore
Requirement
If user_type is pro, show "Pro member". Otherwise, show "Standard member".
Generated handlebar
{{#ifEquals user_type 'pro'}}
Pro member
{{else}}
Standard member
{{/ifEquals}}
Output
Pro member
Requirement
Add up the amount of every item in investments and show it as US dollars.
Generated handlebar
{{formatNumber (sumAll @root 'investments.amount') 'en-US' 'style=currency, currency=USD'}}
Output
$30.00
Requirement
List every investment as a numbered line with the company name and amount in dollars. If there's no company, show "Not known". If there are no investments, say so.
Generated handlebar
{{#each investments}}
{{math @index '+' 1}}.
{{{default this.company 'Not known'}}}
- ${{this.amount}}
{{else}}
No investments were made!
{{/each}}
Output
1. L&T - $10
2. Not known - $20

Not quite right? Regenerate with feedback

Click Try Again to open Re-Generate Handlebar. Refine your requirement and, optionally, choose what went wrong from the Feedback dropdown. Your feedback helps us improve Fyno AI.

Click Generate to try again, or Back to return to the previous result.

If generation fails

If Fyno AI can’t generate a handlebar, you’ll see Oops! That didn’t go as planned. Click Try Again, ideally with a more specific prompt. If an earlier attempt had succeeded, you can also click View previous Handlebar to go back to that result.

Handlebars Builder

The Handlebars Builder lets you build handlebar logic visually, without writing the syntax yourself. To open it from a template, expand Handlebars > Build Your Own in the left panel and click Handlebars Builder.

1

Add blocks

Add blocks from the toolbox (Content, Date & Time, String, Math, Logic, Values and Print) to build your logic.

2

Enter sample data

In Input Object, enter sample data as JSON, such as the sample payload on this page.

3

Check the result

Handlebar Syntax shows the generated handlebar, and Output Preview shows the result for your sample data.

4

Copy the handlebar

Click Copy under Handlebar Syntax and paste the handlebar into your template.