All Collections
Templates
How to manage template scripting
How to manage template scripting

Check out how you can implement template scripting in your emails.

Support Team avatar
Written by Support Team
Updated over a week ago

Script fields are denoted with {{ }}.

Make sure Template Scripting is enabled in your Account's Sending Settings.

Javascript can be used inside the script fields.

Script fields can be used in the subject, HTML, and text body.
โ€‹**Note: due to subject field size limit, it's advised to put the main scripting in the HTML and use short expressions in the subject element.

The scripting engine also includes the Contact and Profile Merge Fields, for example {{ var myname = accountfirstname + ' ' + accountlastname; }}. Fields defined in CSV mail merge are also accessible by their column names. Note that the generic Merge Fields such as {{unsubscribe}} or {{view}} can not be used currently with Template Scripting.

Exceptions

A Javascript code error inside the script field will prevent the email from being sent and will be reported as a bounce. Standard JavaScript try/catch can be leveraged to control error handling.

Additional Functions

  • md5(text) - returns the MD5 hash of given string.

  • download(url) - downloads and returns page content as string. If remote server can't be contacted, an exception is thrown.

  • htmltag(html, tag) - returns value of a given tag, e.g. var subject = htmltag(content, 'title');

Example 1

Subject line

{{ subject }}

HTML

{{
var html = download('http://somewhere.com?recipient=' + encodeURIComponent(email)); // note: email is defined as a part of standard merge field set.
html = html.replace('[-EMAILADDR-]', email);
html = html.replace('04102014', Math.floor(Math.random() * 100000000));

var subject = htmltag(html, 'title');

htmltag(html, 'body'); // this line evaluates to content in the body tag and is returned by this script block
}}

Example 2

Subject line

Today is {{new Date()}}.

HTML

{{
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
null; // this line is so this script block evaluates to null, otherwise it would have evaluated to content of today variable.
}}
<p>This is a page that links to today's <a href="http://dilbert.com/strip/{{today}}">Dilbert</a> strip.</p>

Example 3

Merge fields with alternative display when you do not have data for that merge field.

For example you want to add {firstname} field that will show First Name of your recipient if you have it on file, but will show "Hi there!" if you do not have information about First Name of your recipient.

{{var greeting = "Dear " + firstname + ",";if (firstname.length==0) greeting = "Hi there!";}}
Did this answer your question?