Conditional Content in Email Templates
With Document Studio, you can add conditional content to your emails so that specific sentences or paragraphs are only included when certain criteria are met. For instance, you can show a discount offer only to customers from a particular country, or greet the respondent differently based on their answers in the Google Form.
Conditional content in emails is written with Scriptlets, the templating language of Document Studio. You can use it in both the subject line and the message body of the Send Email task.
Use If and EndIf Tags
Section titled “Use If and EndIf Tags”The {% if %} and {% endif %} tags define a conditional section in your email template. The content between the two tags is only included in the email when the condition is true. If the condition is false, the section is left out.
Say you have a question in your Google Form titled Country. The following template adds a paragraph about shipping only for respondents from the United States.
{% assign country = "{{Country}}" %}{% if country == "United States" %}Your order qualifies for free shipping within the US.{% endif %}The assign tag stores the answer of the {{Country}} field in a variable named country, which is then compared in the if tag. The tags themselves are never included in the email.
Show Alternate Content with Else
Section titled “Show Alternate Content with Else”Use the {% else %} tag to include a different block of content when the condition is false.
{% assign country = "{{Country}}" %}{% if country == "United States" %}Your order qualifies for free shipping within the US.{% else %}International shipping charges will be added at checkout.{% endif %}Combine Multiple Conditions
Section titled “Combine Multiple Conditions”Use the or and and operators to combine conditions. For instance, the following template includes the paragraph if the respondent’s country is either Holland or the Netherlands.
{% assign country = "{{Country}}" %}{% if country == "Holland" or country == "Netherlands" %}Our new store in Amsterdam is now open!{% endif %}Compare Numbers
Section titled “Compare Numbers”Conditions can also compare numbers, such as the answer to an Age question in your Google Form.
{% assign age = "{{Age}}" %}{% if age > 18 or age == 18 %}You are eligible to apply for the membership.{% else %}You must be at least 18 years old to apply.{% endif %}Things to Remember
Section titled “Things to Remember”- Every
{% if %}tag must be closed with a matching{% endif %}tag. - Enclose dynamic fields in double quotes when assigning them to a variable, for example
"{{Country}}". - Comparisons are case-sensitive. Use the downcase filter to compare answers regardless of how they were typed.
{% assign country = "{{Country}}" %}{% assign lowercountry = country | downcase %}{% if lowercountry == "india" %}We're open in India!{% endif %}Learn More about Scriptlets
Section titled “Learn More about Scriptlets”Scriptlets can do a lot more than show or hide content. You can reformat answers, calculate values and repeat content in your email templates.
| Topic | What you can do |
|---|---|
| What are Scriptlets | Learn the basics of filters, tags and dynamic fields |
| Logical Statements | Use elsif, switch case and unless for more complex conditions |
| Dynamic Strings | Capitalize, truncate or set default values for answers |
| Formatting Dates | Display dates in any format |
| Math Operators | Round numbers and do calculations |
| Arrays and Loops | Repeat content for each item in a list |
