Velocity Use Cases
- Accessing the embedded object
- Array iteration (in embedded object / array)
- Call the Nth array element
- Access the index of the current array element
- Convert to lowercase
- URL and HTML escaping
- Date and time formatting
- if/else conditions
Accessing an embedded object
Allows you to access the variables found in the substitutions object.
Substitution example: (parameter global_substitutions — email/send, template/set ; substitutions — email/send):
...
"substitutions":
{
"person":
{
"name":"John",
"secondName":"Doe",
"age":"26"
}
}
Usage example: (parameters body.html, body.plaintext, body.amp, subject, headers, options.unsubscribe_url, from_name — email/send, template/set):
"html":"<h2>Hello, $person.name $person.secondName !</h2>"
Array iteration (in embedded object / array)
Returns and iterates the elements of the array of substitutions.
Substitution example: (parameter global_substitutions — email/send, template/set ; substitutions — email/send):
...
"substitutions":
{
"petList":
[
{
"name":"John",
"price":"30"
},
{
"name":"Harold",
"price":"15"
}
]
}
Usage example: (parameters body.html, body.plaintext, body.amp, subject, headers, options.unsubscribe_url, from_name — email/send, template/set):
"html":
"#foreach( $pet in $petList )
$pet.name for only $pet.price
#end"
Substitution example: (parameter global_substitutions — email/send, template/set ; substitutions — email/send):
...
"substitutions":
{
"DogsAndCats":
[
{
"pet":
[
{
"concreteAnimal":"Brown Cat"
}
]
}
]
}
Usage example: (parameters body.html, body.plaintext, body.amp, subject, headers, options.unsubscribe_url, from_name — email/send, template/set):
"html":
"#foreach( $pet in $DogsAndCats)
#foreach( $concreteAnimal in $pet.pet)
Buy this $concreteAnimal.concreteAnimal
#end
#end"
Call the Nth array element
Сalls the array element with a specified index.
Substitution example: (parameter global_substitutions — email/send, template/set ; substitutions — email/send):
...
"substitutions":
{
"petList":
[
{
"name":"John",
"price":"30"
}
]
}
Usage example: (parameters body.html, body.plaintext, body.amp, subject, headers, options.unsubscribe_url, from_name — email/send, template/set):
"html":
"$petList.get(0).get("name")"
Access the index of the current array element
Returns the current array index.
Substitution example: (parameter global_substitutions — email/send, template/set ; substitutions — email/send):
...
"substitutions":
{
"catsArray":
[
"cat":"black"
]
}
Usage example: (parameters body.html, body.plaintext, body.amp, subject, headers, options.unsubscribe_url, from_name — email/send, template/set):
"html": "#foreach( $cat in $catsArray ) Index of $cat is $catsArray.indexOf($cat) #end"
Convert to lowercase
Converts the string to lowercase.
Substitution example: (parameter global_substitutions — email/send, template/set ; substitutions — email/send):
...
"substitutions":
{
"UpperCASE":"TEST"
}
Usage example: (parameters body.html, body.plaintext, body.amp, subject, headers, options.unsubscribe_url, from_name — email/send, template/set):
"html": "$UpperCASE.toString().toLowerCase()"
URL and HTML escaping
Replaces URL and HTML special characters with the correct entities.
Substitution example: (parameter global_substitutions — email/send, template/set ; substitutions — email/send):
...
"substitutions":
{
"url": "hello here & there",
"html_code":"’bread" & "butter’"
}
Usage example: (parameters body.html, body.plaintext, body.amp, subject, headers, options.unsubscribe_url, from_name — email/send, template/set):
"html": "$esc.html($html_code) $esc.url($url)"
Date and time formatting
Returns the "date" data type in a convenient format for further usage.
Note: the variables for substitutions of the "date" data type should begin with "date_" for correct processing, e.g. "date_Birthdate"
Available date formats:
-
dd/mm/yyyy
-
dd-mm-yyyy hh:mm:ss
-
yyyy/mm/dd HH:mm:ss
-
yyyy mm dd HH:mm:ss
-
ISO 8601 2004-02-12T15:19:21+00:00
-
RFC 2822 Thu, 21 Dec 2000 16:01:07 +0200
Substitution example: (parameter global_substitutions — email/send, template/set ; substitutions — email/send):
...
"substitutions":
{
"date_Birthdate":"01/01/1970"
}
Usage example: (parameters body.html, body.plaintext, body.amp, subject, headers, options.unsubscribe_url, from_name — email/send, template/set):
"html":
"$dateFormat.format('yyyy-M-d', $date_Birthdate)"
$dateFormat.format changes the format of the passed date. The following formats are available:
$date -> Oct 19, 2003 9:54:50 PM
$date.long -> October 19, 2003 9:54:50 PM PDT
$date.medium_time -> 9:54:50 PM
$date.full_date -> Sunday, October 19, 2003
$date.get('default','short') -> Oct 19, 2003 9:54 PM
$date.get('yyyy-M-d H:m:s') -> 2003-10-19 21:54:50
If/else conditions
Allows you to insert if / else conditional statements into the email template.
Substitution example: (parameter global_substitutions — email/send, template/set ; substitutions — email/send):
...
"substitutions":
{
"shoeSize":"5"
}
Usage example: (parameters body.html, body.plaintext, body.amp, subject, headers, options.unsubscribe_url, from_name — email/send, template/set):
"html":
"These shoes are
#if( $shoeSize > 5 )
too big
#elseif( $shoeSize < 5 )
too small
#else
just right
#end
for my brother."