Logic control and looping
Although Mustache is an illogical template engine, you can still use merge fields to conduct basic logic control and looping in HTML templates for billing documents, including invoices, credit memos, and debit memos.
Conditional control
If the input data meets the condition specified by the merge fields, the content defined in the middle of merge fields is displayed in the rendered result.
The following table lists merge field examples with conditional control.
Pseudo-code |
Merge Fields |
Description |
|
---|---|---|---|
Boolean field |
If Invoice.Account.AutoPay == True |
|
If an account has the AutoPay option enabled, the If an account has the AutoPay option disabled, the |
Field with Boolean functions |
If InvoiceItems.IsEmpty |
|
If the InvoiceItems list is empty, the If the InvoiceItems list is non-empty, the |
If Account.AccountNumber == null or Account.AccountNumber.isBlank "Show a blank string" Else
|
|
If the If the |
|
And |
If Account.AutoPay and Balance == 0 |
|
If an account has the AutoPay option enabled and the account’s balance is equal to zero, the automatically paid account with zero balance is displayed in the rendered result. |
Exists |
If Account.Entity__c == French and Hardware Product exists |
The example above is based on the following assumptions and will need to be updated to meet your use case:
|
If an account has a custom field named |
Equals |
If RatePlanCharge.ChargeModel == “Flat Fee Pricing” "Is Flat Fee" Else "Not Flat Fee" |
The assumption for the above code is that it would be copied and pasted into a Charge Details table component. |
If the charge model of a rate plan charge is Flat Fee Pricing, the Otherwise, the |
If RatePlanCharge.ChargeModel == “Per Unit Pricing” Display like Per Unit: $ 70.00 Per License Else Display like Flat Fee: $ 4.50 |
The assumption for the above code is that it would be copied and pasted into a Charge Details table component. |
If the charge model of a rate plan charge is Per Unit Pricing, the text in the format of Otherwise, the text in the format of |
|
Multiple conditions |
If RatePlanCharge.ChargeModel == “Flat Fee Pricing” "Flat Fee Pricing" Else If RatePlanCharge.ChargeModel == “Per Unit Pricing” "Per Unit Pricing" Else If RatePlanCharge.ChargeModel == “Tiered Pricing” "Tiered Pricing" Else “Other Pricing” |
The assumption for the above code is that it would be copied and pasted into a Charge Details table component. |
If the charge model of a rate plan charge is Flat Fee Pricing, the If the charge model of a rate plan charge is not Flat Fee Pricing, the rendered result depends on conditions.
|
Loop control
You can use list section merge fields to achieve looping. The following table lists merge field examples with loop control.
Pseudo-code |
Merge fields |
Description |
|
---|---|---|---|
Loop lists |
for item in Invoice.InvoiceItems |
|
You can use this example to show the charge name and service period of all invoice items in generated invoices. |
Nested loops |
for invoiceItem in Invoice.InvoiceItems |
You can use this example to show all taxation items of every invoice item in generated invoices. |
Nested loops
A nested loop is a loop inside another loop. For example, you might want to show all taxation items for every invoice item.
The pseudocode is as follows:
for invoiceItem in Invoice.InvoiceItems |
To do the same with merge fields in HTML invoice templates, you can use the following merge fields:
{{#Invoice.InvoiceItems}} {{ChargeName}} : {{#TaxationItems}} {{Name}} -- {{TaxAmount}} {{/TaxationItems}} {{/Invoice.InvoiceItems}} |
For example, if you want to show a charge details table for each subscription. One invoice contains multiple subscriptions.
The pseudocode is as follows:
for subscription in Invoice.InvoiceItems.Subscription |
To do the same with merge fields in HTML invoice templates, you can use the following example HTML code. The GroupBy
function transforms an InvoiceItems list into a new list, which consists of two fields only:
-
Subscription.Name
: It is the field name used for grouping. -
_Group
: It is a hard-coded key, which contains a list of InvoiceItems with the same subscription name.
See the GroupBy
function in Functions used in merge fields for more information.
{{#InvoiceItems|SortBy(ServiceStartDate,ASC)|GroupBy(Subscription.Name)}} <h4>Subscription: {{Subscription.Name}}</h4> {{Cmd_Assign(BySubscriptionName,_Group)}} <table class="table-grid u_content_custom_generic_table_1"> <thead><tr> <th style="width:auto; text-align:right;"> Description </th> <th style="width:auto; text-align:right;"> Charge Amount </th> <th style="width:auto;text-align:right; "> Tax </th> <th style="width:auto; text-align:right;"> Total </th></tr></thead> <tbody> {{#BySubscriptionName}} <tr> <td style="">{{ChargeName}}</td> <td style="text-align:right;">{{ChargeAmount}}</td> <td style="text-align:right;">{{TaxAmount}}</td> <td style="text-align:right;">{{#Wp_Eval}}{{ChargeAmount}}+{{TaxAmount}}{{/Wp_Eval}}</td> </tr> {{/BySubscriptionName}} <tr> <td style="text-align:right;">Subtotal</td> <td style="text-align:right;">{{BySubscriptionName|Sum(ChargeAmount)}}</td> <td style="text-align:right;">{{BySubscriptionName|Sum(TaxAmount)}}</td> <td style="text-align:right;">{{#Wp_Eval}}{{BySubscriptionName|Sum(ChargeAmount)}}+{{BySubscriptionName|Sum(TaxAmount)}}{{/Wp_Eval}}</td> </tr> </tbody> </table> {{/InvoiceItems|SortBy(ServiceStartDate,ASC)|GroupBy(Subscription.Name)}} {{/Invoice}}