Templates
Each campaign has a Handlebars template associated to it to help render the final message that is sent to the end user. Each template contains the structure of the design or message as well as placeholders for variables that will be filled in for every user that it is sent to.
Getting Started
Parcelvoy has four different types of templates:
- Text Message
- Push Notification
- Webhooks
Each of which has fields that are rendered as Handlebars templates. They can be customized using a combination of variables and functions.
Template Types:
Email
Email templates are comprised of various fields that are related to the email experience such as subject
, body
, cc
, etc. Each field on the template allows you to use Handlebars to modify the end result. Additionally the html
field is sent to the user to be rendered as HTML and will display any styles included.
Text Message
A text message template is largely just a single field that is broken up into pieces based on its content length. Bare in mind that any Handlebars variables that are filled in will be of variable length and could cause the total content length to grow.
Push Notification
Similar to text messages, a push notification is mainly just a piece of text but can also contain a JSON body as well as a title. All fields can be customized using Handlebars to include functions and variables.
Webhooks
Webhook templates are a wrapper around an HTTP REST request. You can customize headers and body parameters to send a request to pretty much any endpoint you can think of. The body
and headers
fields accept objects, but are not rendered as strings. Each key in each object is instead, so Handlebars is only interpreted at the value level of each key.
Variables
Depending on the incoming data each template will have access to a user
object and an optional event
object.
They can be accessed by key and properties inside of each are accessed using dot notation.
{{ user.email }}
// or
{{ event.custom_property }}
Templates will always have access to the user, but the event property will only have a value if the template is being rendered as a result of an event triggering an action.
In general, we recommend using snake case for properties to be consistent, but there is no rule that says you cant use something else.
All user objects will at a minimum have a series of constant fields present (although they may be null) as well as any custom fields you add to the user.
// User Object
{
"id": "YOUR_EXTERNAL_ID",
"email": "abc@example.com",
"phone": "+11234567890",
"locale": "en",
"timezone": "America/Chicago"
}
Helpers
Alongside being able to access variables using Handlebars, you can also modify values using custom helpers. We've included a list of them below.
Equality
Compare
There is a general equality function compare
which lets you check if two values match an operator. Available operators are ==
, ===
, !=
, !==
, <
, >
, <=
and >=
.
Equals
Not Equals
Less Than
Greater Than
Less Than or Equal To
Greater Than or Equal To
Or
The or
function lets you group chunks of logic together by checking if any of the included chunks are true.
And
The and
function lets you group chunks of logic together by checking if all of the included chunks are true.
Strings
Append
Appends the given string
with the specified suffix
.
Camelcase
camelCase the characters in the given string
.
Capitalize
Capitalize the first word in a sentence.
Capitalize All
Capitalize all words in a string.
Ellipsis
Truncates a string to the specified length
, and appends it with an el;ipsis, …
.
Lowercase
Lowercase all characters in the given string.
Occurences
Return the number of occurrences of substring
within the given string
.
Prepend
Prepends the given string
with the specified prefix
.
Replace
In a given piece of text, replace all occurrences of substring a
with substring b
.
Replace First
Replace the first occurrence of substring a
with substring b
.
Reverse
Reverses a string.
Snakecase
Converts a given string to snake case (i.e. snake_case)
Split
Split a piece of text into an array on the given character
.
Starts With
Tests whether a string begins with the given prefix.
Trim
Removes extraneous whitespace from the beginning and end of a string.
Truncate
Truncate a string to the specified length
Truncate Words
Truncate a string to have the specified number of words.
Numbers
Absolute Value
Return the magnitude of a
.
Add
Return the sum of a
plus b
Ceil
Round a decimal value up to the closest whole number.
Divide
Divide a
by b
Floor
Round a decimal value down to the closest whole number.
Subtract
Return the difference of a
minus b
.
Multiply
Return the product of a
times b
. You can pass in an unlimited number of values to multiply together.
Random
Generate a random number between two values
Remainder
Get the remainder when a
is divided by b
.
Round
Round the given decimal number to the closest whole number
Number Format
Format a string given the provided criteria. This is useful for formatting a number so that it makes sense given the context or the users locale.
Format: {{ numberFormat value locale style currency }}
Parameters:
value
: The number to formatlocale
: The users locale to display the number accordinglystyle
: Eithercurrency
orpercent
or blank. Adding this parameter will modify the number to be displayed accordinglycurrency
: You must set thestyle
tocurrency
for this to take effect. If set the currency symbol will be included in the formatted value.
Examples:
Lists
Wrap
Takes a given value and if it's not a array (list), it wraps the value in an array to guarantee that the response will always be an array.
First
Returns the first item of a list.
Is Array
Returns true if the value is an array (list).
Item At
Return the item from a list at the provided index. Note, the index starts at zero not one.
Join
Join all elements of a list into a string, optionally using a given separator.
Last
Returns the last item of a list.
Length
Returns the number of items in the provided list.
Reverse
Reverse the elements in a list, or the characters in a string.
Sort
Sort the given list in the provided order.
Format: {{ sort array direction }}
Parameters:
array
: The list of items to be sorteddirection
: What direction to sort the array, accepted values are eitherasc
for ascending ordesc
for descending.
Dates
Now
Returns the current date time in the format and timezone provided.
Format: {{ now format timezone }}
Parameters:
format
: The date format to use for the returned value. Defaults toyyyy-MM-dd HH:mm:ss
timezone
: What timezone to use for the returned value. Defaults toUTC
Format
Takes a given date and formats it to the provided format and timezone.
Format: {{ dateFormat format timezone }}
Parameters:
format
: The date format to use for the returned value. Defaults toyyyy-MM-dd HH:mm:ss
timezone
: What timezone to use for the returned value. Defaults toUTC
Set
Set a given component of a date to a provided value.
Format: {{ setDate date unit value }}
Parameters:
date
: The date you are wanting to manipulate.unit
: The component unit of a date (years
,months
,days
,hours
,minutes
).value
: How many of the given unit as a number.
Addition
Perform date math on a given date by adding some unit to the date.
Subtraction
Perform date math on a given date by subtracting some unit to the date.
Next Date
Get the date of the next occurance of a given day of week from a provided date.
Format: {{ nextDate date day }}
Parameters:
date
: The date to use as the starting point.day
: The two letter abbreviation of the day of the week to seek (mo
,tu
,we
,th
,fr
,sa
,su
).
Date Difference
Get the distance between two days in the provided unit.
Format: {{ dateDiff date date2 unit }}
Parameters:
date
: The later datedate2
: The earlier dateunit
: What unit to get the difference in (iehours
,minutes
)
Urls
Encode URI
Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character.
Escape
Escape the given string by replacing characters with escape sequences. Useful for allowing the string to be used in a URL, etc.
Decode URI
Decode a Uniform Resource Identifier (URI) component.
Subscription
Unsubscribe Email
Get a url to unsubscribe a user from a given email subscription channel.
Preference Center
Get a url to navigate a user to a page where they can manage what subscription channels they are subscribed to.