Scriptlets Reference: Conditions, Filters and Loops in Mail Merge
A scriptlet is a short instruction that you type inside your template. It can show text only when a condition is true, change a value before it is printed, do arithmetic, or repeat a line for each item in a list. Mail Merge runs the scriptlets for each row, after it fills in the merge fields.
Scriptlets use Liquid, a template language that many email and website tools share. You don’t need to know how to program. Most templates need only two or three of the tags on this page.
The same scriptlets work in Mail Merge, Form Notifications and Document Studio. A template that you write for one add-on works in the other two.
The Two Forms of a Scriptlet
Section titled “The Two Forms of a Scriptlet”| Form | Written as | What it does | Example |
|---|---|---|---|
| Tag | {% … %} |
Makes a decision, repeats text or stores a value. A tag prints nothing itself. | {% if {{Plan}} == "Pro" %} |
| Output | {? … ?} |
Prints a value, often after a filter has changed it | {? {{Your name}} | upcase ?} |
Merge fields keep their double curly braces inside a scriptlet. The output form uses {? and ?} because {{ and }} already mean a merge field.
The Example on This Page
Section titled “The Example on This Page”Maya runs Willow Lane Studio. Her members sheet has these columns, and every example uses Sofia’s row.
| Column | Sofia’s cell |
|---|---|
Your name |
sofia marquez |
Plan |
Pro |
Seats |
3 |
Amount |
49.5 |
City |
Lisbon |
Notes |
(empty) |
Interests |
Pottery, Weaving, Glass |
Event date |
Mar 14, 2027 |
Bio |
Maker of small things, teacher, and a slow runner who likes long walks |
Conditions
Section titled “Conditions”if, elsif and else
Section titled “if, elsif and else”{% if %} shows the text up to {% endif %} only when the condition is true. {% else %} gives the text for every other case, and {% elsif %} adds another condition in between.
{% if {{Plan}} == "Team" %}Your team space is ready.{% elsif {{Plan}} == "Pro" %}Your Pro membership is active.{% else %}You are on the free plan.{% endif %}Sofia reads Your Pro membership is active.
Comparisons
Section titled “Comparisons”| You write | True for Sofia | True when |
|---|---|---|
{% if {{Plan}} == "Pro" %} |
Yes | The cell is exactly Pro |
{% if {{Plan}} == "pro" %} |
No | Capital letters must match |
{% if {{Plan}} != "Free" %} |
Yes | The cell is anything else, or empty |
{% if {{Seats}} > 2 %} |
Yes | The cell is a number above 2 |
{% if {{Seats}} >= 3 %} |
Yes | 3 or more |
{% if {{Seats}} < 3 %} |
No | A number below 3 |
{% if {{Seats}} <= 3 %} |
Yes | 3 or less |
{% if {{Notes}} == "" %} |
Yes | The cell is empty |
{% if {{Notes}} != "" %} |
No | The cell is not empty |
{% if {{Interests}} contains "Weaving" %} |
Yes | The text is somewhere in the cell |
Text goes in straight quotes. Numbers have no quotes. A cell that looks like a number is a number, so {{Seats}} == 3 is true and {{Seats}} == "3" is false.
and, or
Section titled “and, or”{% if {{Plan}} == "Pro" and {{Seats}} > 5 %}You qualify for the group rate.{% endif %}
{% if {{Plan}} == "Pro" or {{Plan}} == "Team" %}Thanks for being a paying member.{% endif %}Sofia has 3 seats, so she reads only the second sentence.
unless
Section titled “unless”{% unless %} is the opposite of {% if %}. The text shows when the condition is false.
{% unless {{Plan}} == "Free" %}Your invoice is attached.{% endunless %}case and when
Section titled “case and when”{% case %} compares one cell with a list of values. It is shorter than a long chain of {% elsif %}.
{% case {{Plan}} %}{% when "Free" %}Upgrade any time from your account page.{% when "Pro", "Team" %}Your next invoice is on the 1st.{% else %}Contact us to set up your plan.{% endcase %}A {% when %} can list several values with commas between them.
Empty Values
Section titled “Empty Values”| You write | Result |
|---|---|
{% if {{Notes}} == "" %} |
True when the cell is empty. Use this form. |
{% if {{Notes}} != "" %} |
True when the cell has something in it |
{% if {{Notes}} %} |
True for every row, even when the cell is empty. Don’t use this form. |
A merge field with no matching column also counts as empty.
Print and Change a Value
Section titled “Print and Change a Value”{? … ?} prints a value. A filter after a | changes the value first. Filters can follow each other, and each one works on the result of the one before:
Hi {? {{Your name}} | split: " " | first | capitalize ?},Text Filters
Section titled “Text Filters”| You write | Output | What it does |
|---|---|---|
{? {{Your name}} | upcase ?} |
SOFIA MARQUEZ |
Capital letters |
{? {{Your name}} | downcase ?} |
sofia marquez |
Small letters |
{? {{Your name}} | capitalize ?} |
Sofia marquez |
A capital first letter, small letters after |
{? {{Your name}} | append: "!" ?} |
sofia marquez! |
Adds text at the end |
{? {{Your name}} | prepend: "Dr. " ?} |
Dr. sofia marquez |
Adds text at the start |
{? {{Your name}} | replace: "sofia", "Sofia" ?} |
Sofia marquez |
Replaces every match |
{? {{Your name}} | replace_first: "a", "A" ?} |
sofiA marquez |
Replaces the first match only |
{? {{Your name}} | remove: "sofia " ?} |
marquez |
Removes every match |
{? {{Your name}} | slice: 0, 5 ?} |
sofia |
5 characters, starting at the first |
{? {{Your name}} | size ?} |
13 |
The number of characters |
{? {{Bio}} | truncate: 30 ?} |
Maker of small things, teac... |
Cuts to 30 characters, including the dots |
{? {{Bio}} | truncatewords: 5 ?} |
Maker of small things, teacher,... |
Cuts to 5 words |
{? " hi " | strip ?} |
hi |
Removes spaces at both ends |
{? {{Notes}} | default: "None" ?} |
None |
Text to use when the cell is empty |
{? {{City}} | url_encode ?} |
Lisbon |
Makes text safe to put in a link |
{? "<b>Hi</b>" | strip_html ?} |
Hi |
Removes HTML tags |
{? "<b>Hi</b>" | escape ?} |
<b>Hi</b> |
Shows HTML as text |
Also available: lstrip, rstrip, strip_newlines, remove_first, url_decode and escape_once.
A full name with capital letters needs a short loop, because capitalize changes only the first letter:
{% assign words = {{Your name}} | split: " " %}{% for word in words %}{? word | capitalize ?} {% endfor %}Number Filters
Section titled “Number Filters”| You write | Output | What it does |
|---|---|---|
{? {{Seats}} | plus: 1 ?} |
4 |
Adds |
{? {{Seats}} | minus: 1 ?} |
2 |
Subtracts |
{? {{Seats}} | times: 20 ?} |
60 |
Multiplies |
{? {{Amount}} | times: {{Seats}} ?} |
148.5 |
Multiplies two cells |
{? {{Amount}} | divided_by: 4 ?} |
12.375 |
Divides |
{? 7 | modulo: 2 ?} |
1 |
The remainder of a division |
{? {{Amount}} | round ?} |
50 |
Rounds to a whole number |
{? 3.14159 | round: 2 ?} |
3.14 |
Rounds to 2 decimal places |
{? {{Amount}} | ceil ?} |
50 |
Rounds up |
{? {{Amount}} | floor ?} |
49 |
Rounds down |
{? -5 | abs ?} |
5 |
Removes the minus sign |
{? {{Seats}} | at_least: 5 ?} |
5 |
The value, or 5 if the value is lower |
{? {{Seats}} | at_most: 2 ?} |
2 |
The value, or 2 if the value is higher |
Things to watch with numbers:
- A number with a comma, a currency sign or a unit is text.
$1,200is not above 1000. Remove the signs first:{? "$1,200" | remove: "$" | remove: "," | plus: 0 ?}gives1200. - There is no filter that always prints two decimals.
148.5stays148.5, not148.50. - Arithmetic with decimals can give a long tail, such as
0.30000000000000004. End withround: 2. - To add a currency sign, use
prepend:{? {{Amount}} | times: {{Seats}} | prepend: "$" ?}gives$148.5.
Date Filters
Section titled “Date Filters”The date filter prints a date in the format that you give.
| You write | Output |
|---|---|
{? {{Event date}} | date: "%B %d, %Y" ?} |
March 14, 2027 |
{? {{Event date}} | date: "%A, %-d %b %Y" ?} |
Sunday, 14 Mar 2027 |
{? {{Event date}} | date: "%d/%m/%Y" ?} |
14/03/2027 |
{? {{Event date}} | date: "%Y-%m-%d" ?} |
2027-03-14 |
{? {{Event date}} | date: "%A" ?} |
Sunday |
{? "now" | date: "%B %Y" ?} |
The current month and year |
| Code | Meaning | Example |
|---|---|---|
%Y |
Year, 4 digits | 2027 |
%y |
Year, 2 digits | 27 |
%B |
Month name | March |
%b |
Short month name | Mar |
%m |
Month number, 2 digits | 03 |
%d |
Day, 2 digits | 14 |
%-d |
Day, no leading zero | 14 |
%A |
Weekday name | Sunday |
%a |
Short weekday name | Sun |
%H |
Hour, 24-hour clock | 09 |
%I |
Hour, 12-hour clock | 09 |
%M |
Minutes | 05 |
%p |
AM or PM | AM |
To add days to a date, turn it into seconds, add, and format it again. One day is 86400 seconds:
Reply by {? {{Event date}} | date: "%s" | plus: 604800 | date: "%b %e, %Y" ?}A scriptlet gets a date as the cell shows it. The date filter can read 2027-03-14, Mar 14, 2027 and 3/14/2027. It can’t read a date that starts with the day, such as 14/03/2027. "now" is the moment the email is sent, in UTC, not in your spreadsheet’s time zone.
A list is several values in one cell, such as Pottery, Weaving, Glass. split turns the text into a list, and the list filters work on it.
| You write | Output | What it does |
|---|---|---|
{? {{Interests}} | split: ", " | join: " + " ?} |
Pottery + Weaving + Glass |
Joins the items with other text |
{? {{Interests}} | split: ", " | size ?} |
3 |
Counts the items |
{? {{Interests}} | split: ", " | first ?} |
Pottery |
The first item |
{? {{Interests}} | split: ", " | last ?} |
Glass |
The last item |
{? {{Interests}} | split: ", " | sort | join: ", " ?} |
Glass, Pottery, Weaving |
Sorts the items |
{? {{Interests}} | split: ", " | reverse | join: ", " ?} |
Glass, Weaving, Pottery |
Reverses the order |
Also available: uniq removes repeated items, concat joins two lists, sum adds a list of numbers and sort_natural sorts without regard to capital letters.
{% for %} repeats the text up to {% endfor %} once for each item in a list. Store the list with {% assign %} first.
{% assign interests = {{Interests}} | split: ", " %}You chose:{% for item in interests %}{? forloop.index ?}. {? item ?}{% endfor %}Sofia reads:
You chose:1. Pottery2. Weaving3. Glass| Inside a loop | What it gives |
|---|---|
{? forloop.index ?} |
The number of this round, from 1 |
{? forloop.length ?} |
The number of items |
{% if forloop.first %} |
True in the first round |
{% if forloop.last %} |
True in the last round |
{% break %} |
Stops the loop |
{% continue %} |
Skips to the next item |
{% else %} before {% endfor %} |
Text to show when the list is empty |
| Loop | Output for Sofia |
|---|---|
{% for item in interests limit: 2 %} |
Pottery, Weaving |
{% for item in interests offset: 1 %} |
Weaving, Glass |
{% for item in interests reversed %} |
Glass, Weaving, Pottery |
{% for i in (1..3) %} |
1, 2, 3 |
{% for i in (1..{{Seats}}) %} |
1, 2, 3, because Sofia has 3 seats |
A loop over numbers prints one line for each seat:
{% for i in (1..{{Seats}}) %}Seat {? i ?} of {? {{Seats}} ?}{% endfor %}Variables
Section titled “Variables”{% assign %} stores a value under a name that you choose. Use the name later without braces.
{% assign total = {{Amount}} | times: {{Seats}} %}Your total is ${? total ?}.{% if total > 100 %}You get free delivery.{% endif %}Sofia reads Your total is $148.5. and You get free delivery.
A filter can’t go inside {% if %}. Store the filtered value first:
{% assign plan = {{Plan}} | downcase %}{% if plan == "pro" %}Thanks for going Pro.{% endif %}This also makes a comparison ignore capital letters.
{% capture %} stores a block of text, with its merge fields and scriptlets filled in:
{% capture greeting %}Hi {? {{Your name}} | split: " " | first | capitalize ?}{% endcapture %}{? greeting ?}, welcome to the studio.{? greeting ?}, your invoice is attached.Other Tags
Section titled “Other Tags”| Tag | What it does |
|---|---|
{% comment %} … {% endcomment %} |
A note to yourself. Nothing between the tags is printed. |
{% raw %} … {% endraw %} |
Prints the text between the tags as typed, without running it |
{% echo {{Plan}} | upcase %} |
The same as {? {{Plan}} | upcase ?} |
{% cycle "odd", "even" %} |
Inside a loop, prints the next value of the list in each round |
{% increment n %}, {% decrement n %} |
Prints a counter that goes up or down each time |
{% tablerow item in list %} … {% endtablerow %} |
Prints the items as cells of an HTML table row |
Remove Extra Blank Lines
Section titled “Remove Extra Blank Lines”A tag on a line of its own leaves an empty line where it stood. Add a hyphen inside the tag to remove the white space on that side:
{%- if {{Plan}} == "Pro" -%}Pro member{%- endif -%}Not Available
Section titled “Not Available”{% include %},{% render %}and{% layout %}load other template files. They are turned off, and a template that has one fails withtag "include" not found.- A cell is never read as a scriptlet. If a cell holds
{% if %}, the output shows that text. - A merge field can’t hold a scriptlet inside its braces.
Where Scriptlets Work in Mail Merge
Section titled “Where Scriptlets Work in Mail Merge”| Place | Scriptlets run |
|---|---|
| Message, in every editor | Yes |
| Subject | Yes |
| Sender name | Yes |
| The plain text part of the email | Yes |
| Send to, CC, BCC and Reply-to | No |
Conditional Text shows how to add a condition to a campaign step by step. Change Values with Filters does the same for filters.
Tips for Sheet Values
Section titled “Tips for Sheet Values”- A scriptlet reads the text of a cell. A cell in bold or in colour compares by its text.
- In the message, what
{? … ?}prints is shown as text. HTML that a filter writes, such as the<br>fromnewline_to_br, appears as typed. - An Embed marker inside an
{% if %}runs only when the condition is true.{% if {{Resume}} != "" %}{{Embed File, Resume}}{% endif %}attaches the file only for the rows that have one. - A template has one second to run. A loop over a very long list can pass that limit.
When a Scriptlet Has a Mistake
Section titled “When a Scriptlet Has a Mistake”Mail Merge sends nothing for a row whose template has a scriptlet mistake. The row’s Mail Merge Status cell shows Scriptlet error in the template: followed by the reason. When the template itself can’t be read, as with a tag that is never closed, the campaign stops at the first row.
Fix the template and run the campaign again. Mail Merge sends the rows that failed and skips the rows that were sent. Send a test email after every change to a scriptlet.
Troubleshooting
Section titled “Troubleshooting”The condition is never true
Section titled “The condition is never true”- Check the capital letters.
Prois notpro. To ignore them, store the value withdowncasefirst. - Check for quotes. Text needs straight quotes, as in
"Pro". Without quotes, the condition is false and no error shows. - For
>and<, the cell must be a plain number with no comma, currency sign or unit. - A number has no quotes.
{{Seats}} == "3"is false when the cell is the number 3.
“expected “|” before filter”
Section titled ““expected “|” before filter””The tag has something that Liquid can’t read. The usual causes are curly quotes such as “Pro”, a filter inside an {% if %}, or a tag that is partly bold or coloured. Type the tag again as plain text with straight quotes.
“tag {% if … %} not closed”
Section titled ““tag {% if … %} not closed””An {% if %}, {% for %}, {% case %}, {% unless %} or {% capture %} has no closing tag. Each one needs its own {% endif %}, {% endfor %}, {% endcase %}, {% endunless %} or {% endcapture %}.
The condition with “and” and “or” gives the wrong result
Section titled “The condition with “and” and “or” gives the wrong result”Liquid reads and and or from right to left and has no brackets. Split the condition into two {% if %} tags, one inside the other.
The date filter prints the date unchanged
Section titled “The date filter prints the date unchanged”The filter can’t read the date. It reads forms such as 2027-03-14 and Mar 14, 2027. It can’t read 14/03/2027.
The output has empty lines
Section titled “The output has empty lines”Each tag on its own line leaves a line break behind. Use the hyphen form, {%- … -%}, or put the tag on the same line as the text.
