Article
· Oct 18, 2019 2m read

Tool for creating odt and docx documents from templates

Hello everyone,

I want to introduce you my project.

There was a common task in my work to create documents, in which I need insert some information about person: full name, birth date, address and etc. I wrote the application to do it in the simplest way.

The idea is not brand new and was implemented in other languages. I wanted to make a similar tool for Cache Object Script. The concept is following.

You create a template document in LibreOffice or Microsoft Word using special semantics.

Than write a method or procedure in which all variables used in template are defined. The method must be declared with [ProcedureBlock = 0] keyword, otherwise undefined error will occur while rendering template.

In method use the command  to render document:

set error =  ##class(DocumentTemplate).RenderDocumentFromTemplate("<full-path-to-your-template>","<full-path-to-your-rendered-document>")

Templates are simple ODT or DOCX documents.

To print a varibles or expressions type double curly braces enclosing the variable, like

{{ firstName }} was born in {{ $E($ZD(date,8),1,4)) }} year.

The style of generated content will be the same style as style of the first curly brace.

Tool can repeat parts of the template (rows of table, paragraphs, simple words) using subscripted variables.  For example:

This will be rendered to:

While rendering 'index'  will be declared and used for iteration through row variable.  List items in the example above are red and cursive, because of first brace style. List items and rows with tags are removed in result document, because they don’t contain any other characters.  “If” tag is used for conditional output and takes any valid COS expression. “Order” and “if” tags may be nested in each other.

Also I implemented a “set” tag. You can assign variables right inside the template and use them throughout the document. I used it for row enumeration.

Unfortunately, it doesn’t work perfectly well as I expected. Mixing template tags and normal text into the template document may ruin the layout and produce invalid document. But for the simple cases it works fine.

https://github.com/azenkov1991/DocumentTemplate

Discussion (2)0
Log in or sign up to continue

Hi Artem!

Very cool!

I've just tested it on docker (build a repo, see the PR) - and it works like a charm!

For docker funs it I set all the settings in Global during container building, so you just need to build a container and then you can run tests already.  

So git clone/download the repo and then run in the folder:

$ docker-compose -f "docker-compose.yml" up -d --build

Open IRIS terminal and run tests:

$ docker-compose exec iris iris session iris
USER>do ##class(DocumentTemplate.Test).RunAllTests("/iris/app/TestDocs")

Collect the results in /Results folder of the repo.  Tested on Mac OS with IRIS 2019.3 CE, store/intersystems/iris-community:2019.3.0.309.0