Skip to content

Easy to use Template Library Available in Many Programming Languages

License

Notifications You must be signed in to change notification settings

InductiveComputerScience/pbTpl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pbTpl - pb Templates

A template library available in many programming languages made with progsbase. The goals of this library are to

  1. Have a single library for templating that is stable accross programming languages and over time.
  2. Is easy to build in any programming language and to include in any project.
  3. Is easy to use.

Try Online

Try the library online here.

Available in the Following Languages

Main Function

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.

General About Templates

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.

Tags and Their Functions

Print Value

{print a}

Will print the value of a. If it is a JSON object or structure, the JSON repressentation will be printed.

Use structure

{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.

Iterate

{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-else

{if a}
{else}
{end}

If a is true, the first block is printed, else the else block. The else block is optional.

Escape

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.

How to Work with Templates

There are three main issues with using templates:

The data model

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.

Templates

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.

Locale and Formatting

Special formatting of numbers should be taken care of during data model generation. This is better as a full fledged programming language is available.