Templating syntax
Here you'll undestand how to write template for further uploading it to your stack and then call the API to get pdf return with filled data.
⚠️ Currently there is no validation for template available. So read this documentation careful before creating templates
Supported commands
Currently supported commands are defined below.
QUERY
You can use GraphQL, SQL, whatever you want: the query will be passed unchanged to your data
query resolver.
+++QUERY
query getData($projectId: Int!) {
project(id: $projectId) {
name
details { year }
people(sortedBy: "name") { name }
}
}
+++
For the following sections (except where noted), we assume the following dataset:
const data = {
project: {
name: 'docx-templates',
details: { year: '2016' },
people: [{ name: 'John', since: 2015 }, { name: 'Robert', since: 2010 }],
},
};
INS
(=
, or nothing at all)
Inserts the result of a given JavaScript snippet:
+++INS project.name+++ (+++INS project.details.year+++)
or...
+++INS `${project.name} (${$details.year})`+++
Note that the last evaluated expression is inserted into the document, so you can include more complex code if you wish:
+++INS
const a = Math.random();
const b = Math.round((a - 0.5) * 20);
`A number between -10 and 10: ${b}.`
+++
You can also use this shorthand notation:
+++= project.name+++ (+++= project.details.year+++)
+++= `${project.name} (${$details.year})`+++
LINK
Includes a hyperlink with the data resulting from evaluating a JavaScript snippet:
+++LINK ({ url: project.url, label: project.name })+++
If the label
is not specified, the URL is used as a label.
HTML
Takes the HTML resulting from evaluating a JavaScript snippet and converts it to Word contents.
Important: This uses altchunk, which is only supported in Microsoft Word, and not in e.g. LibreOffice or Google Docs.
+++HTML `
<meta charset="UTF-8">
<body>
<h1>${$film.title}</h1>
<h3>${$film.releaseDate.slice(0, 4)}</h3>
<p>
<strong style="color: red;">This paragraph should be red and strong</strong>
</p>
</body>
`+++
FOR
and END-FOR
Loop over a group of elements (resulting from the evaluation of a JavaScript expression):
+++FOR person IN project.people+++
+++INS $person.name+++ (since +++INS $person.since+++)
+++END-FOR person+++
Note that inside the loop, the variable relative to the current element being processed must be prefixed with $
.
It is possible to get the current element index of the inner-most loop with the variable $idx
, starting from 0
. For example:
+++FOR company IN companies+++
Company (+++$idx+++): +++INS $company.name+++
Executives:
+++FOR executive IN $company.executives+++
- +++$idx+++ +++$executive+++
+++END-FOR executive+++
+++END-FOR company+++
FOR
loops also work over table rows:
----------------------------------------------------------
| Name | Since |
----------------------------------------------------------
| +++FOR person IN | |
| project.people+++ | |
----------------------------------------------------------
| +++INS $person.name+++ | +++INS $person.since+++ |
----------------------------------------------------------
| +++END-FOR person+++ | |
----------------------------------------------------------
Finally, you can nest loops (this example assumes a different data set):
+++FOR company IN companies+++
+++INS $company.name+++
+++FOR person IN $company.people+++
* +++INS $person.firstName+++
+++FOR project IN $person.projects+++
- +++INS $project.name+++
+++END-FOR project+++
+++END-FOR person+++
+++END-FOR company+++
IF
and END-IF
Include contents conditionally (depending on the evaluation of a JavaScript expression):
+++IF person.name === 'Guillermo'+++
+++= person.fullName +++
+++END-IF+++
Similarly to the FOR
command, it also works over table rows. You can also nest IF
commands
and mix & match IF
and FOR
commands. In fact, for the technically inclined: the IF
command
is implemented as a FOR
command with 1 or 0 iterations, depending on the expression value.
ALIAS
(and alias resolution with *
)
Define a name for a complete command (especially useful for formatting tables):
+++ALIAS name INS $person.name+++
+++ALIAS since INS $person.since+++
----------------------------------------------------------
| Name | Since |
----------------------------------------------------------
| +++FOR person IN | |
| project.people+++ | |
----------------------------------------------------------
| +++*name+++ | +++*since+++ |
----------------------------------------------------------
| +++END-FOR person+++ | |
----------------------------------------------------------