Introduction to Scriptlets - Templating Language for Document Studio
Document Studio supports Scriptlets, an email templating language that lets you include sophisticated logic with conditional operators. For instance, use the if/else filter to conditionally render some content if a specified condition is true. If the condition is false, no content is rendered or another block of content may be rendered.
You can also create dynamic content or reformat content using a simple syntax. For instance, respondents of a Google Form may enter their name in all lowercase format (e.g. “angus”), but you may want to display the names in their original format with the first letters capitalized (e.g. “Angus”). This is where Scriptlets come in.
Filters
Section titled “Filters”Filters change the output of a scriptlet. They are delimited by {? and ?}. When the template is rendered, the filter is replaced with the evaluated value of the filter.
For instance, the following scriptlet will capitalize the first letter of the first word of the input text.
{? "angus" | capitalize ?}Multiple filters can be used on one output, separated by the pipe(|) character, and are executed in the order they appear from left to right. The output of the previous filter will become the input of the next filter in the chaining.
For instance, the following scriptlet will round the input number to two decimal places and then prepend the output with custom text.
{? 183.929 | round: 2 | prepend: "The due amount is $" ?}In the next example, the expression will replace all occurrences of the word color with colour in the output.
{? "My favorite color is blue" | replace: "color", "colour" ?}| Filter Group | Available Filters |
|---|---|
| String Manipulation | default, append, prepend, slice, capitalize, upcase, downcase, truncate, truncatewords |
| Array Operations | size, split, join, concat, reverse, uniq, sort, sort_natural, first, last |
| Date Formats | now, today |
| Math Operations | round, times, abs, ceil, floor, plus, minus, divided_by, modulo, at_least, at_most |
| Replace & Remove | replace, replace_first, remove, remove_first, strip, rstrip, lstrip, strip_newlines |
| HTML Entities | escape, escape_once, url_encode, url_decode, newline_to_br |
Tags create the logic and control the flow for your scriptlets. They are delimited by {% and %}. Any text that is surrounded by {%...%} is treated as a tag and is not included in the output.
For instance, the scriptlet below takes a string containing a list of fruit names and prints the first two fruit names in separate lines.
{% assign fruitArray = "Apple, Orange, Mango" | split: ", " %}{% for fruit in fruitArray limit:2 %} {? fruit ?}{% endfor %}Tags can also be used to print conditional content. For instance, the following scriptlet will add the relevant salutation based on the answer entered by the respondent for the question Input your gender in the Google Form.
{% assign gender = "{{Input your gender}}" %}{% assign lowergender = gender | downcase %}{% if lowergender == "female" %} Hello Ms {{First Name}}{% else %} Hello Mr {{First Name}}{% endif %}| Tag Group | Available Tags |
|---|---|
| Conditional Tags | if else, if elsif, switch case, unless |
| Control Loops | for in, limit, offset, break, continue, increment, decrement |
Dynamic Fields
Section titled “Dynamic Fields”The general syntax of any Scriptlet expression is {? <input text> | <filter 1> | <filter 2> | ... ?}. The text enclosed within the {?...?} delimiters becomes the output, after being acted upon by all filters in sequence.
The input text can also be replaced by dynamic fields (column headers) using the double curly bracket syntax.
For instance, if you have a column in your Google Sheet titled Full Name, you can use the following scriptlet to display the first name of each respondent prepended with a greeting.
Hello {? {{Full Name}} | split: " " | first | append: "!" ?}Use Dynamic Fields in Tags
Section titled “Use Dynamic Fields in Tags”A dynamic field can also go inside a tag, to show text only for the rows where a column has a certain value. The examples use a sheet with the columns Full Name, City, Age and Coupon.
{% if {{City}} == "Paris" %} Your nearest store is on Rue de Rivoli.{% else %} We deliver to {{City}} in three days.{% endif %}| You write | The text shows when |
|---|---|
{% if {{City}} == "Paris" %} |
The City cell is Paris. Capital letters must match. |
{% if "{{City}}" == "Paris" %} |
The same. Quotes around the field also work. |
{% if {{Age}} >= 18 %} |
The Age cell is a number, 18 or above |
{% if {{Coupon}} != "" %} |
The Coupon cell is not empty |
{% if {{City}} contains "Par" %} |
The cell has those letters anywhere in it |
Text goes in quotes and numbers don’t. To compare two numbers, leave the quotes off the field as well, as in {{Age}} >= 18.
Things to Know
Section titled “Things to Know”- A number with a comma, a currency sign or a unit is text.
1,200and$25can’t be compared with>or<. Keep a column of plain numbers for the condition. - An empty cell is not “false”.
{% if {{Coupon}} %}is true for every row. Compare with""to check for an empty cell. - A value from your sheet or form is never read as a scriptlet. If a cell holds
{% if %}, the output shows that text. A quote or an apostrophe in a cell, as inO'Brien, does not break a condition. - Type tags as plain text, with straight quotes. Curly quotes like
“Paris”, or bold or coloured text inside a tag, cause an error. {% include %},{% render %}and{% layout %}are not available.
Troubleshooting
Section titled “Troubleshooting”The task shows an error and no email is sent
Section titled “The task shows an error and no email is sent”A scriptlet in the email subject or message has a mistake, so Document Studio stops that task instead of sending the template with its tags showing. The error starts with Scriptlet error in the template: and then says what to fix:
| After the colon, it says | Check this |
|---|---|
tag {% if … %} not closed |
An {% if %} has no {% endif %} after it. |
tag "endif" not found |
An {% endif %} has no {% if %} before it. |
expected "|" before filter |
The tag has curly quotes or formatted text. Type it again as plain text. |
tag "include" not found |
The tag is not available in Document Studio. |
Fix the template and run the workflow again for that row.
The output shows the tags as typed
Section titled “The output shows the tags as typed”There is a space between { and %, so the tag is read as ordinary text. Type it again as {% if … %}.
The condition is never true
Section titled “The condition is never true”Check these in order:
- Does the value in the quotes match the cell, with the same capital letters and no extra space?
- Is the text in quotes, like
"Paris"? Without quotes, the condition is never true and no error is shown. - For
>or<, does the cell hold a plain number?
