A template library available in many programming languages made with progsbase. The goals of this library are to
- Have a single library for templating that is stable accross programming languages and over time.
- Is easy to build in any programming language and to include in any project.
- Is easy to use.
Try the library online here.
The library is available in the programming languages above. The main function for using a template is as follows:
TypeScript:
function GenerateDocument(template : string [], json : string [], document : StringReference, errorMessage : StringReference) : boolean
Java:
boolean GenerateDocument(char [] template, char [] json, StringReference document, StringReference errorMessage)
C:
_Bool GenerateDocument(wchar_t *template, size_t templateLength, wchar_t *json, size_t jsonLength, StringReference *document, StringReference *errorMessage)
C++:
bool GenerateDocument(vector<wchar_t> *templatex, vector<wchar_t> *json, StringReference *document, StringReference *errorMessage)
JavaScript:
function GenerateDocument(template, json, document, errorMessage)
PHP:
function GenerateDocument(&$template, &$json, $document, $errorMessage)
C#:
bool GenerateDocument(char [] template, char [] json, StringReference document, StringReference errorMessage)
Python:
def GenerateDocument(template, json, document, errorMessage)
Ruby:
def GenerateDocument(template, json, document, errorMessage)
Visual Basic:
Function GenerateDocument(ByRef template As Char (), ByRef json As Char (), ByRef document As StringReference, ByRef errorMessage As StringReference) As Boolean
This function works exactly the same way in all the languages. It takes a template string template
and JSON string json
. It returns true on success or false on failure. On success, document.string
is set to the result; on failure errorMessage.string
is filled with an error message.
A textual template is text with certain parts left out that will be filled in by a generation step. The source for filling in is a data structure generated from a JSON file.
Templates are used for many different things in production systems. Most websites are generated based on a template, be it directly in the front end, such as with Angular, React and Vue, or in the backend. Templates are also used for production of formal documents and emails.
{print a}
Will print the value of a. If it is a JSON object or structure, the JSON repressentation will be printed.
{use a}
{print b}
{end}
This will cause a.b to be printed. Inside the use structure, the variables inside structure a can be used.
{foreach a in b}
{end}
This will iterate over all elements of the array b
. Each element will be known as a
inside. If a
is an object, it will automatically be used inside the foreach loop, so there is no need to write {use a}
.
{if a}
{else}
{end}
If a is true, the first block is printed, else the else block. The else block is optional.
If you want to output {
followed directly by one of the command strings, use a backslash in front. If the {
is not followed directly by one of the commands, the text is simply output.
There are three main issues with using templates:
Generate all the data that is to be filled into the document. This includes decision variables if some information is included or left out based on conditions.
Write the template with the tags to fill in information. There is no need for complex logic or formatting commands, as this should be taken care of during data model generation. The reason is that the code should be in the code and not in the template.
Special formatting of numbers should be taken care of during data model generation. This is better as a full fledged programming language is available.