Introduction

What is the Emarsys Scripting Language?

The Emarsys Scripting Language (ESL) is a scripting language for creating dynamic content in your email campaigns, based on predefined rules. As opposed to our standard personalization options, which can populate email content based only on contact database fields, ESL lets you personalize content based on external factors, too.

It is written specifically for people with a knowledge of HTML.

How does this compare to Twig?

Twig does share similarities with the Emarsys Scripting Language, but there are huge differences as well. While Twig is a language used solely for email templates and uses functions only to substitute variables in the template (e.g. make all first names uppercase), the Emarsys Scripting Language is a genuine scripting language. As well as being used for templates, it can also handle more complex algorithms.

It also offers a universal solution for handling personalization, conditional text, and block targeting, and is simpler and more flexible than our current methods.

Please note that ESL does not support all Twig tags and filters, so you are advised to use this documentation and not include elements from the Twig documentation referenced above.

Why should I use ESL?

Since it is a scripting language, it provides numerous functions for you. First of all, the personalizations you can create with it can be more complex than the standard personalization options options in Emarsys. Secondly, you can adjust any rule based on the values of your personalization fields for a specific email campaign.

Examples

You can use ESL in the following cases:

Custom controls - You can display a button called Become a member to only those contacts who are not members of your loyalty scheme. Although this could be done via simple conditioning or section targeting as well, for those who know HTML, it makes email creation a lot easier.

Counters - You can place a counter into your email without having to put a URL into your HTML code.

Conversions - You can handle transformations from one contact field to another, for example, to display field values provided in kilometers in miles instead.

Loops - You can create a loop, and do not have to refer to each and every array element separately. As ESL is a scripting language, it can automatically go through the array and use all elements one by one.

All in all, customization of your contacts becomes easy and fully automatic with the Emarsys Scripting Language.

Editor-specific details

The Emarsys Scripting Language is supported only by custom HTML email campaigns and by email campaigns created in the new Visual Content Editor. Old template-based campaigns do not support it.

Language Reference

Terminology

{{ this is an expression }}
{% this is a statement %}
|this is a filter

Tags

An if statement checks if something evaluates to true. If it does, the block content up to the (mandatory) {% endif %} is processed. This can be a part of the HTML code:

{% if "@example.com" in contact.3 %}
this is an example.com email address
{% else %}
this is not an example.com email address
{% endif %}

In this example above, we are looking for system field #3, which is the email address. If the contact has an example.com email address, it is displayed so. Otherwise it is stated that it has a different address.

{% if contact.5 == 1 %}
this is for him
{% else %}
this is for her
{% endif % }

In this example we are targeting our customers based on their gender. We can show different products to them, based on the above query.

In this example, the split character is , (comma). item loops through all elements from the array and lists them one by one. If you include the HTML <br> tag, every element will be displayed in a new row:

one
two
three

You can also include a limit argument. There is a difference between working with positive and negative numbers. An example for a positive number is 2, where the last 2 elements will become one string and the remaining elements will stay separately.

Result

Result

You can cut and display a part of an array or string with the slice filter. If it is an array, foreach is used with it. Its first parameter defines from which element it starts (note that the very first element is signed by 0), and the second parameter defines the number of elements to cut (note that it includes the starting element as well). In the next example, the starting element of an array is 1, so it will start from the second element, and the number of elements is 2, so it will cut 2 elements starting with the second element.

Result

2
3

An example for slicing a string is the following:

{{ '12345'|slice(2, 2) }}

Result

34

If the starting number is negative, counting starts from the end of the array or string (e.g. -4 will sign the second element, which is 2 in the above example). If no second parameter is provided, it means including all upcoming elements. Please note that zero cannot be a second parameter.

{{ '12345'|slice(-4) }}

Result

The values of string template placeholders can be defined with the format filter. These placeholders are %s (string) and %d (number). In the following example, the value 10 is set for the message number. The format parameter is added with this variable as a parameter. The result is having this number in the string.

Result

You can encode a URL or part of a URL which contains invalid URL characters. This means that the invalid URL characters will be replaced by their valid HTML character combinations. An example for a URL particle containing a * (asterisk):

{{ "seg*ment"|url_encode }}

Result

seg%2Ament

Another example is having one or more spaces in the URL:

{{ "one space"|url_encode }}

Result

one%20space

In the next example, a key-value pair is converted into valid URL parameters:

{{ {'key': 'value', 'foo': 'bar'}|url_encode }}

Result

The characters valid in HTML can be escaped with the escape filter. An example is when the First name field of the contacts is checked. If any contains an HTML character (e.g. <), it will be treated as a simple string.

Result

Result

Result

It displays the number of array elements or the string character number.

{{ [1, 2, 3, 4]|length }}

Result

4

{{ '1234'|length }}

Result

4

The length filter can have a parameter and it can be included within if tags. In the following example, the length parameter 10 defines that if the number of users is more than 10, the provided message is shown.

{% if users|length > 10 %}
The number of users is more than 10.
{% endif %}

Result

This formats decimal numbers. The first parameter of number_format determines how many numbers the decimal part contains. In the following example, it is 2, so 2 decimals will be displayed from the 3. The second parameter defines the decimal separator, which is , (comma) in this case. The third parameter stands for the thousands separator, which is . (period) here.

{{ 9800.333|number_format(2, ',', '.') }}

Result

9.800,33

Please note that if no parameter if given for the number_format filter, the decimals are not displayed at all and the thousands separator is , (comma).

{{ 2005.35|number_format }}

Result

{{ "I like this and that."|replace({'this': 'chocolate', 'that': 'candy'}) }}

Result

I like chocolate and candy.

Please note that if the very same string part is provided as a value first, then a key in the parameter, it will become overwritten. In this example, you can see that "cats" changes to "dogs" first, then both "dogs" change to "birds".

Result

Result

The rounding conditions can be defined with the help of the round filter. Its first parameter defines the number of decimals, which is 1 in our example. As for the second parameter, two options exist. The first is floor, which rounds the numbers down regardless of the common rounding rules.

{{ 42.58|round(1, 'floor') }}

Result

42.5

The second is ceil, which rounds the numbers up in any case.

{{ 42.54|round(1, 'ceil') }}

Result

42.6

Please note that the default is having no decimals and using common rounding (rounding the value up from decimal 5 and rounding it down under it).

Result

It is a date formatting function. It displays the date of the specified country and it can also translate this date (it can handle the PHP language translations). Please note that it displays only the date, even if the time is also provided. It can be overwritten, so the predefined date format becomes displayed (e.g. [{locale: en, format: yyyy mm}] means that the format YYYY MM will become visible for English people.) The localized_date filter can have 4 parameters. These are displayed in the order defined by their language settings, and contain the following:

full: year, month, date, day of the week

long: year, month, date

medium: year, abbreviated version of month, date

short: YYYY/MM/DD, the separator can be different.

The input string can have the following formats:

'YYYY-MM-DD'

'now'

'+[number] hours'

'+[number] days'

'-[number] hours'

'-[number] days'

In this example, a full version is provided on the basis of the stored country+language value in contact field 12 for the specified date (e.g. it will become January 1 Friday, 1999).

{{ '1999-01-01'|localized_date(contact.12, 'full') }}

You can also use a database field placeholder to populate the date dynamically. In you do this, make sure to leave out the single quotes around the field placeholder, as the date value will be returned in quotes. In this example we use value of contact field 12345 to provide the date.

It is a time formatting function. It displays the time of the specified country and it can also translate this time (it can handle the PHP language translations). Please note that it displays only the time, even if the date is also provided. Here you can find what is displayed exactly if you provide parameters.

full: HH:MM:SS (AM/PM)

long: HH:MM:SS (AM/PM)

medium: HH:MM:SS (AM/PM)

short: HH:MM (AM/PM)

{{ '1999-01-01 13:13:13'|localized_time(contact.12, 'medium') }},

Please note here as well that the input string has a compulsory form (HH:MM:SS for time). In this example, a time is generated from a datetime with the localized_time filter on the basis of the stored country+language value in contact field 12 (13:13:13).

It is a date and time formatting function. It displays the date and time of the specified country and it can also translate this (it can handle the PHP language translations). Regarding its parameters, it puts the values of the above two filters together (e.g. if its parameter is full, the date and time will be displayed as full as well).

If the evaluation does not have a result and this filter is added, the email campaign will not be sent. It will be displayed on the Analysis page in Emarsys though. The default invalid values for this filter are "" (empty string), and the value null.

{{ contact.2|required(['-']) }}

The parameter of this filter (the string - in this case) overwrites the default invalid empty string, so this string will become the new invalid one. You can still include the empty string later as well as it is possible to define more than one invalid values. For example, ([x], [y], [ ]) means that there are 3 invalid elements, the last one is the empty value.

Personalization

{{ contact. }}

Contact data is migrated from the Emarsys database. Here you can find the list of the possible email personalization placeholders:

{{ rds. }}

The data here comes from the Relational Data Service (RDS). Product-recommendation is a view which relates RDS data from different tables. Its parameter is a field on the basis of which you identify the contact (it is the email address here).

Result

t-shirt
trouser

Please note here that the parameter can only be a constant (a field or a string), it must remain the same.

Useful Tools

Twig testing is possible with TwigFiddle, where the result of any twig can be checked. Note here that we have some unique tags, parameters and filters, for which you cannot use this testing method easily. If you wish to test code with the foreach tag, we suggest that you replace it with a simple fortag, so that you can check your code. Other elements which cannot be tested here are the limitparameter and the .rds-related codes. From filters, the four unique ones are:

localized_date

localized_time

localized_datetime

required

For editing your script, Atom and Notepad++ can be useful with a twig-specific add-on.

Samples

External Data for Transactional Emails

If you want to create transactional emails in Visual Content Editor, you need to have the following:

With the above JSON example, order 1234, its related products 12 and 13 and their images will be available for the email campaign. The data displayed in the email itself will be gathered from here. You can include any number of items.

You have the possibility to test your JSON on the API Demo API Demo page. You need to include the data part of your JSON in the text field of data and define the remaining parameters.

You can check how the Triggering an External Event API endpoint works here.

With the help of the above HTML snippet, a header containing the order ID is displayed in the transactional email. The code contains the foreach tag, which loops through all product codes and image URLs provided in the JSON. The limit 5 restrictions means that the email will list the first five product codes and image URLs.

Please note that the default and maximum limit is 100.

The very last step is to finalize your campaign and activate it in the Automation Center or use it as a simple external event triggered message.