EJS (Express.js) is a web application framework for Node.js. In BILLmanager EJS is implemented with the /usr/local/mgr5/etc/scripts/template_ejs.js script. For correct operation, the incoming XML-file is converted to a JSON file.
The EJS language describes document and message templates in BILLmanager. To add or edit templates, navigate to Settings → Document templates.
Operators allow handling various scenarios to generate a printing form of a document. The system checks conditions and executes a code based on the result.
This article describes the following operations:
- comparison operator;
- logical operator;
- the if ... else operator;
- conditional operator;
- the switch operator.
Comparison operator
This is a binary operator that takes two operands in which values are being compared.
Logical operators
Logical operators combine multiple boolean values and provide a single boolean output.
- && (AND) — this operator will be truthy if the expressions on both sides of it are true.
- || (OR) — this operator will be truthy if the expression on either side of it is true.
- ! (NOT) — this operator converts the operand to boolean type and returns an inverse value.
A rule of logical expression in a code is represented by the keywords true and false. True is returned if a condition is true, and false is returned if a condition is false.
if ... else operator
Syntax:
if (logical expression) {
piece of code 1 //it will be executed if the lofical expression is true.
} else {
piece of code 2 //it will be executed if the lofical expression is false.
}
The use of else is optional:
if (logical expression) {
piece of code 1 //it will be executed if the lofical expression is true.
}
piece of code 2 //it will be executed in any case regardless the logical expression.
If one condition may result in several scenarios, you can use else if. The following example shows a condition to display cents in the total amount due:
if (_second_num == 1 & _first_num != 1) _string += ' cent'; //The first condition. If it is true, the described code will be applied.
else if (_second_num > 1 && _second_num < 5) _string += ' cents'; //The second condition. The system checks that the first condition is false.
else _string += ' cents';// It is executed if none of the conditions are true.
Example
You can add payer information using if ... else in the standard invoice template:
<% if ((findu(payment.customer.profiletype) == 2) || (findu(payment.customer.profiletype) == 3)) { %> // If a payer is a company
<div class="title">Customer: </div>
<div class="company"><%= findu(payment.customer.name) %></div>
<div>VAY<%= findu(payment.customer.vatnum) %>, IBAN<%= findu(payment.customer.iban) %></div> // Show VAT and IBAN in a document.
<div class="line"> </div>
<div><%= findu(payment.customer.postcode_legal) %>, <%= findu(payment.customer.country_legal_name) %>,
<%= findu(payment.customer.city_legal) %>, <%= findu(payment.customer.address_legal) %></div>
<% } else { %> // Otherwise a payer is an individual
<div class="title">Customer: </div>
<div class="company"><%= findu(payment.customer.person) %></div>
<div class="line"> </div>
<div><%= findu(payment.customer.postcode_physical) %>, <%= findu(payment.customer.country_physical_name) %>,
<%= findu(payment.customer.city_physical) %>, <%= findu(payment.customer.address_physical) %></div> // Show the customer address in a document.
<% } %>
In the code above, the system checks a payer type. If it is a company, its VAT number and IBAN will be displayed in the document. Otherwise, a customer is considered an individual, and the document will show his address.
Conditional operation.
A conditional operator is an alternative to if... else. Conditional operators return one value if the condition is true and returns another value if the condition is false.
Syntax:
(condition)?piece of code 1:piece of code 2
The system applies the piece of code 1 if the condition is true. If the condition is false, it applies the piece of code 2.
Example
The following function converts a digit into a string:
var n=!isFinite(+number)?0:+number
It can be explained as "if the expression+number is not a finite number, the n variable is assigned 0, otherwise n is assigned +number".
switch operator
The switch operator excepts a variable or expression and executes a required piece of code depending on the result. Its basic syntax:
switch (expression) {
case 1:
piece of code 1
break; // completed the case operator
case 2: // there can be multiple cases (variants )
piece of code 2
break;
default: // it is executed if the values doesn not match any result.
piece of code 3
}
The piece of code 1 is applied if the result is 1. The piece of code 2 is applied if the result is 2, and the piece of code 3 if applied if the result is another number except for 1 and 2.
Example
Let's describe the code that returns one of four sentences depending on the input value. This code is not used in a pre-made template in the billing system.
switch (word) {
case 'hi':
phrase = 'Hello my dear friend';
break;
case 'bye':
phrase = 'Goodbye, i hope to see you again';
break;
case 'joke':
phrase = 'I dont know jokes';
break;
case 'day':
phrase = 'Have a nice day!';
break;
default:
phrase = 'I dont understand you';
}
The above code compares the values of the input variable word with possible values:
- if "word" contains the word "hi", the variable"phrase" will be assigned the value "Hello my dear friend";
- if "word" contains "bye", the variable"phrase" will be assigned the value "Goodbye, I hope to see you again";
- if "word" contains "joke", the variable"phrase" will be assigned the value "I don't know jokes";
- if "word" contains "day", the variable"phrase" will be assigned the value "Have a nice day";
- if the values do not match, the variable"phrase" will be assigned the value "I don't understand you".