Repository navigation
Mustache templating engine
Voodoo has a templating engine which allows you to inject dynamic values into a response body before it is returned. This is applicable to responses that return text (JSON, YAML, text).
The engine is a mustache engine which many developers will at least recognise from it's signature curly brackets ({{...}}). Mustache is basically a logic less templating system for injecting values into text. Anywhere the curly brackets appear, mustache will look at their contents and compare it to a data model it has received. Finding matching values, it will then replace the brackets and their contents with the values it has found.
Mustache can inject simple values, perform basic if-then-else injection based on boolean values, compose the contents of multiple files and iterate over arrays.
To setup the engine there are several arguments that can be passed to Voodoo in it's configuration. The first is the template directory. This is the directory where mustache templates will be read from. The second is the extension used on those template files. By default it's .json however you can change it using the template extension setting.
The templates are keyed based on the file names. So if you have configured the templates directory as the template directory, and added the template templates/cart/cart.json, then it's template name will be cart/cart.
The data model is generated by Voodoo each time it needs to run the engine and it's populated with data from a number of sources:
-
Server data. At the moment this only consists of a single value:
-
mockServer- This is set to the headerHostvalue from the incoming request. So if the incoming request specifies made the call to192.168.0.4:8080then that value this will be set as themockServervalue in the template data so it can be injected into response payloads to correctly reference the server.
-
-
All the data currently residing in the pan-request cache.
-
Any additional data added by the endpoint definition. Note this data overrides anything in the cache that has the same key.
As an example if we have the following files in the template directory:
Books.json
{
"title": "{{listTitle}}",
"numberOfBooks": {{nbrBooks}},
"books": [
{{> iab1}},
],
"reviews": [
{{#reviews}}
"{{comment}}",
{{/reviews}}
]
}Note that the use of mustache tags means this is invalid JSON at this point in time.
Iab1.json
{
"id": "1000-01",
"name": "Consider Phlebas",
"author": "Ian M. Banks",
"price": 25
}And we ask the template engine to process the Books template with this model:
{
"title": "My book list",
"numberOfBooks": 1,
"reviews": [
{ "comment": "Amazing",
{ "comment": "Fantastic"
]
}Then the response would be:
{
"title": "my book list",
"numberOfBooks": 1,
"books": [
{
"id": "1000-01",
"name": "Consider Phlebas",
"author": "Ian M. Banks",
"price": 25
},
],
"reviews": [
"Amazing",
"Fantastic",
]
}Whilst it's possible to template YAML content, you should be careful with any multi-line data to ensure that it ends up correctly indented after rendering.