Expressions: Mustache templates
Key takeaways
MustacheTemplate | PIP.Services component used to construct and evaluate Mustache templates. |
Basic constructors | Helpers used to construct more complex templates. |
template | MustacheTemplate 's property used to create templates. |
evaluate | MustacheTemplate 's method used to validate Mustache templates. |
Introduction
PIP.Services offers an implementation of a Mustache engine available in its Expressions module. This implementation of Mustache is enhanced with the addition of some helpers. In this tutorial, you will learn how to use the MustacheTemplate component, which can be used to evaluate Mustache templates. First, we will see its pre-requisites, then, we will learn some basic constructions supported by the component. Finally, we will see some examples of its usage.
Pre-requisites
In order to use the Mustache template library, we must first install the Expressions module by running the following command:
npm install pip-services4-expressions-node --save
Managing Mustache templates
PIP.Services uses a set of basic constructions to create Mustache-like templates. In this section, we will learn how to use them to build and evaluate different templates.
Basic constructions
The library supports the following injections and conditional blocks:
Variable | {{{NAME}}} |
Conditional (if) | {{ #if VARIABLE }}Some value or {{{VARIABLE2}}} {{/if}} |
Conditional negation (if – else) | {{{#unless VARIABLE}}} Some value or {{{VARIABLE}}} {{{/unless}}} |
If-else equivalent | {{ #if VARIABLE }} Some value or {{{VARIABLE}}} {{/if}}{{{^VARIABLE}}} Some value or {{{VARIABLE}}} {{{/VARIABLE}}} |
Pre-requisites
To use Mustache templates, we must import the MustacheTemplate class. The following command shows how to do this:
import { MustacheTemplate } from "pip-services4-expressions-node";
Examples
Below are some examples of evaluations:
a) Variable
Variables that have a dictionary structure can be used to validate a template. The following example shows how to use the evaluate_with_variables method.
import { MustacheTemplate } from "pip-services4-expressions-node";
export function main() {
// variable
let template = new MustacheTemplate();
template.template = "Hello, {{{NAME}}}";
let variables = { "NAME": "Alex" };
let result = template.evaluateWithVariables(variables);
console.log(result);
}
Which produces the following output:
![figure 1](figure1.png)
b) Conditional
To create a conditional template, we use the #if helper. An example of its usage will be a template that creates the message “Hello, (Name)”. The variable used has two fields namely name and exclamation. The last one represents a Boolean value which if it is set to true, the message will show an added exclamation mark.
Note: In general, any value that is interpreted by a specific language as false when the “If” operator is executed will be interpreted as false. Otherwise, it is considered true. For example, in Python, false values are None, False, 0, and ‘’. Node.js adds an empty list to them.
The following example shows how to create a conditional template:
import { MustacheTemplate } from "pip-services4-expressions-node";
export function main() {
// variable
let template = new MustacheTemplate();
template.template = "Hello, {{{NAME}}}{{ #if EXCLAMATION }}!{{/if}}";
let variables = { "NAME": "Alex" , "EXCLAMATION": "1" };
let result = template.evaluateWithVariables(variables);
console.log(result);
}
After running the above code, we will see the following result:
![figure 2](figure2.png)
And, if we set the exclamation to None, we will get a message without an exclamation mark:
import { MustacheTemplate } from "pip-services4-expressions-node";
export function main() {
// variable
let template = new MustacheTemplate();
template.template = "Hello, {{{NAME}}}{{ #if EXCLAMATION }}!{{/if}}";
let variables = { "NAME": "Alex" , "EXCLAMATION": false };
let result = template.evaluateWithVariables(variables);
console.log(result);
}
![figure 3](figure3.png)
c) Conditional negation (if-else)
Now, we want to modify our program to define a template that if the exclamation value is equal to false, it adds a dot at the end. Otherwise, it ends with an exclamation mark.
In the first case, our code is:
import { MustacheTemplate } from "pip-services4-expressions-node";
export function main() {
// "if else" construction
let template = new MustacheTemplate();
template.template = "Hello, {{{NAME}}}{{ #if EXCLAMATION }}!{{/if}}{{{^EXCLAMATION}}}.{{{/EXCLAMATION}}}";
let variables = { "NAME": "Alex" , "EXCLAMATION": false };
let result = template.evaluateWithVariables(variables);
console.log(result);
}
And the result is:
![figure 4](figure4.png)
In the second case, we just modify the value of EXCLAMATION to true,
import { MustacheTemplate } from "pip-services4-expressions-node";
export function main() {
// "if else" construction
let template = new MustacheTemplate();
template.template = "Hello, {{{NAME}}}{{ #if EXCLAMATION }}!{{/if}}{{{^EXCLAMATION}}}.{{{/EXCLAMATION}}}";
let variables = { "NAME": "Alex" , "EXCLAMATION": "1" };
let result = template.evaluateWithVariables(variables);
console.log(result);
}
and we get a string ended in an exclamation mark.
![figure 5](figure5.png)
d) If-else equivalent
We can also create an equivalent if-else structure by using the #unless helper. The following code shows how to do this:
import { MustacheTemplate } from "pip-services4-expressions-node";
export function main() {
// equivalent constructions
let template = new MustacheTemplate();
template.template = "Hello, {{{NAME}}}{{{#unless EXCLAMATION}}}.{{{/unless}}}";
let variables = { "NAME": "Alex" , "EXCLAMATION": "1" };
let result = template.evaluateWithVariables(variables);
console.log(result);
}
![figure 6](figure6.png)
e) Using default variables
We can also assign default variables to a template and use the evaluate function to check their values. The following is an example of this:
import { MustacheTemplate } from "pip-services4-expressions-node";
export function main() {
// equivalent constructions
let template = new MustacheTemplate();
template.template = "Hello Mr, {{{NAME}}} {{{SURNAME}}}";
let variables = { "NAME": "Joe", "SURNAME": "Smith" , "EXCLAMATION": false };
for(let key in variables)
template.defaultVariables[key] = variables[key];
let result = template.evaluate();
console.log(result);
}
Which will result in:
![figure 6](figure6.png)
Moreover, as variables have a dictionary structure, we can also define the default value as:
template.defaultVariables['NAME'] = 'Joe';
template.defaultVariables['SURNAME'] = 'Smith';
f) Clearing the template
The class offers the clear method to erase all the information stored in a template. The syntax is as follows:
template.clear();
Wrapping up
In this tutorial, we have seen how to manage basic Mustache templates. We have learned the basic constructions handled by PIP.Services and seen examples of each of them. We have also learned how to use default variables and how to clear a MustacheTemplate object.