TemplateMark is a data format from Accord Project that describes formatted natural language with embedded variables. In this article I introduce TemplateMark, the problems it solves and how to use it as an application developer.

Here is a very simple TemplateMark document:

Hello {{message}}! Your name is **{{% return message.length %}}** characters long.

This template can be passed to the Accord Project Template Engine to produce output documents from the template + incoming JSON data. For example, this markdown output document:

Hello World! Your name is **5** characters long.

Will be produced by the template when fed the following JSON data:

{
    $class: 'helloworld@1.0.0.TemplateData',
    message: 'World',
}

TemplateMark variables are enclosed in {{ and }} while calculations are enclosed in {{% and %}}. Calculations are TypeScript expressions. Text formatting uses markdown syntax — and in fact TemplateMark is a superset of the CommonMark markdown format, so any CommonMark markdown document is a valid TemplateMark document.

The template above includes a single variable, named message and based on the subsequent calculation this variable appears to be of type string (a character array).

TemplateMark templates must be explicitly associated with a Concerto data model, which supplies the type information for all the variables in the template. Here is the data model for the template above:

namespace helloworld@1.0.0
@template
concept TemplateData {
    o String message
}

As expected the TemplateData concept which is annotated with the @template decorator includes a single property with the name message, of type String.

The association between a TemplateMark document and it’s Concerto model is vital as it allows the structure of the incoming data for the template to be validated and also for the template to be type-checked, ensuring that all the variable references are valid, including the syntax of the calculations. This is critical as it guarantees that the template produces valid output for a broad range of valid inputs.

Run the code for this article online

https://replit.com/@dselman/AccordProjectTemplateEngine

We’ve just scratched the surface with this first article, and there’s a lot more material to cover:

  1. Parsing TemplateMark markdown to TemplateMark DOM JSON format
  2. Template Engine interpreter vs compiler
  3. Concerto data model for TemplateMark itself
  4. Conditional sections in TemplateMark
  5. Expanding Concerto arrays into markdown lists
  6. Conversion of AgreementMark output JSON to HTML, markdown, PDF…

We will cover those topics in a follow-up article.