Collections and Looping
In addition to simple token replacement, certain data comes in the form of a collection. For example, the signup confirmation contains a list of subscription items that are part of the subscription, since there could be more than one. To incorporate collection data into a template, it is necessary to use a looping construct token. The example below illustrates the syntax of looping though a list of items in a collection, in this case the subscription items part of a subscription:
$SubscriptionItems: {item | $item.ProductName$$\n$}$
If a subscription includes two products A and B, then the token above would produce the following text in its place when the notification is generated:
Product A
Product B
Conditionals
It is also possible to show a certain text portion only if a condition is true. This can be done using an if-endif token. The example below illustrates how to include the customer service phone number only if it has been configured:
$if(CustomerServicePhone)$Phone: $CustomerServicePhone$$endif$
Formatting
Token data may also be formatted according to a set of rules. For example, you may want to show a date in a certain format and left or right align it within the notification. The example below shows how to format the balance right aligned within a space of 11 characters:
$Balance;format=”{11:C}”$
The format string consists of an open bracket, an alignment value (optional), a colon, one or more format code(s) (optional), and a close bracket.
Alignment Value
An optional integer indicating the minimum width of the region to contain the formatted value. If the length of the formatted value is less than alignment, then the region is padded with spaces. If alignment is negative, the formatted value is left justified in the region; if alignment is positive, the formatted value is right justified. If alignment is not specified, the length of the region is the length of the formatted value. The colon is required if alignment is specified.
Format Code(s)
An optional string of formatting codes. Valid formatting codes for numbers:
Code | Format Name | Example |
c | Currency | $1.42 |
d | Decimal (Whole number) | -12400 |
e | Scientific | 1.42E+00 |
f | Fixed point | 1.42 |
g | General | 1.42 |
n | Number with commas for thousands | 1.42 |
r | Round trippable | 1.42 |
Valid formatting codes for dates:
Code | Format Name | Example |
d | Short date | 10/12/2002 |
D | Long date | 10-Dec-02 |
t | Short time | 10:11 PM |
T | Long time | 10:11:29 PM |
f | Full date & time | 12/10/2002 22:11 |
F | Full date & time (long) | 12/10/2002 22:11 |
g | Default date & time | 10/12/2002 22:11 |
G | Default date & time (long) | 10/12/2002 22:11 |
M | Month day pattern | 10-Dec |
r | RFC1123 date string | Tue, 10 Dec 2002 22:11:29 GMT |
s | Sortable date string | 2002-12-10T22:11:29 |
u | Universal sortable, local time | 2002-12-10 22:13:50Z |
U | Universal sortable, GMT | 12/11/2002 3:13 |
Y | Year month pattern | December, 2002 |
yyyy/M/d | Four digit year, month, day | 2002/5/21 |