How do I use Variables in Targeted Email?
Variables let you create your own custom placeholder values in a Targeted Email — values that aren’t available as standard merge fields. For example, if several affiliates use the same email template but each needs a different organization name in the content, you can create a variable for the organization name so each affiliate only needs to change it in one place.
Note: Variables are case-sensitive, cannot contain spaces, can only use alphanumeric characters, and cannot start with a number. Variable values can include special characters, emojis, and spaces. Variables can be used in the query string portion of a URL (after the ?), but not in the domain or path portion.
Step 1: Set a single-value variable
Use the set() command directly after the <body> tag in your email (or inside a <noscript> tag to keep it hidden from the WYSIWYG editor):
<noscript> {{set(‘org_name’,‘People For Good’)}} </noscript>
We recommend placing your set() commands after the <body> tag. Variables set in the <head> section won’t carry over to the plain text version of your email automatically.
Step 2: Use the variable in your email content
Reference your variable anywhere in the email using the get() command:
Welcome from {{get(‘org_name’)}}!
Step 3: Create an array variable (list of values)
Use set() with curly braces to create a list:
{{set(‘my_array’,{‘1’,‘2’,‘3’,‘4’})}}
Step 4: Loop through an array
Use {{each}} and {{end}} to display each value, along with loop_var (current value), loop_index (position in the list), and #get(‘array_name’) (total count of items):
{{each get(‘ask_amounts’)}} Ask amount #{{loop_index}}: {{loop_var}} {{end}}
Step 5: Handle nested loops
If you nest {{each}} loops, {{loop_var}} resets each time a new loop runs. To preserve the value from an outer loop, save it to a new variable before starting the inner loop:
{{each get(‘array1’)}} {{set(‘my_loop_value’,loop_var)}} {{each get(‘array2’)}}{{loop_var}},{{end}} My value: {{get(‘my_loop_value’)}} {{end}}
Step 6: Create a conditional list
To display array values as a comma-separated list, use the total count to detect when you’ve reached the last item:
{{set(‘animals’,{‘cat’,‘dog’,‘bird’,‘mouse’})}} {{set(‘animals_length’,#get(‘animals’))}} {{each get(‘animals’)}} {{loop_var}} {{if equals(get(‘animals_length’),loop_index)}}{{else}},{{end}} {{end}}
This produces: cat, dog, bird, mouse
Not quite what you were searching for?
- Navigate back to the Understanding Email Personalization Resource Index.
How do I use variables in Targeted Email in EveryAction? | How do I create a custom merge field in EveryAction? | How do I create an array in Targeted Email? | How do I loop through a list in Targeted Email? | How do I use the set and get commands in EveryAction email? | How do I create a comma-separated list in Targeted Email? | How do I make a custom placeholder in EveryAction Targeted Email?
