template

The template service stores template files using the Nunjucks template language which are used to create documents from the JSON data sent to the service. In the user interface the template service is shown as a tree of template files which you can create, edit and delete. Reading and writing these files is done with GET and PUT requests. To use the service to create a document from a JSON data object, POST the object to the service and the output document is returned.  Templates use the resource name convention <resourcename>.<output doc suffix>.njk, for instance mypage.html.njk would be a template called mypage outputting html.

The following extension filters are provided to Nunjucks:

  • whereEquals(property, value): filters a list/array of objects down to those where the given property equals the given value, e.g. {{ itemsList | whereEquals("key", "abc") }} would filter itemsList down to those items where the value of the property key was "abc".
  • dateFormat(format): formats a date expressed as a string into another date format. This uses the moment npm package, so you can refer to its documentation to see the formats it can parse and the format strings it uses to display a date.

Data preprocessing

The template service preprocesses JSON data in a few ways to make it more useable and flexible.

  • Because Nunjucks has no means of referring to the whole data context, and it only lets you refer to properties of the data context, if an array or primitive JSON value is passed into the template service, it automatically wraps it in an object with a single property called $this. You can change the name of this property by giving it as the value of the query string parameter dataProp. Similarly if you POST to a template with a text type body e.g. HTML, XML or text/plain, this will be passed in to the template as a data context with a single key, data whose value is a string containing the text. This can be useful for instance for wrapping a fragment of HTML in a page master template.
  • To allow values to be passed in via the url, all query arguments on the url with which the service was called are mapped as keys and values onto the data context. So if you call a template like /templates/my-template?urlKey=123, you can then put {{ urlKey }} into your template and it will be substituted with 123.