YumTemplate
YumTemplate is the main entry point to Yumdocs
A typical use involves the following:
import {YumTemplate} from '@yumdocs/yumdocs';
const t = new YumTemplate();
await t.load('./input.docx');
await t.render({field: 'Anything you see fit'});
await t.saveAs('./output.docx');
For more advanced uses, requiring custom configuration, check extensions.
constructor(options: Object)
After importing YumTemplate, you first need to create a new instance, as in:
const t = new YumTemplate();
You can pass an options object to set tag delimiters and locale.
const t = new YumTemplate({
delimiters: {
start: '{',
end: '}'
},
locale: 'fr-FR'
});
The default options are:
{
delimiters: {
start: '{{',
end: '}}'
},
locale: 'en-US'
}
load(template: Blob|Buffer|File|string)
Then you need to load your Microsoft Office template, as in:
await t.load('./input.docx');
A template can be loaded from:
- a Blob object,
- a nodeJS Buffer,
- a File object, or
- a string, i.e. a local file path, but not a remote url.
You can use axios to load a template from a remote url as follows:
import axios from 'axios';
const response = await axios({
method: 'get',
responseType: 'blob',
url: 'https://localhost/templates/input.docx'
});
await t.load(response.data);
For more information about available options, refer to zip.loadAsync since most options are forwarded, except a string is evaluated as a file path in nodeJS.
render(data: Object)
The render method merges the template loaded into a YumTemplate instance with an object.
The following line of code replaces any occurrence of {{field}}
with its value, Anything you see fit
.
await t.render({field: 'Anything you see fit'});
You can use axios to render a json object from a restfull API as follows:
import axios from 'axios';
const response = await axios({
method: 'get',
responseType: 'json', // default
url: 'https://localhost/data/1234567890'
});
await t.render(response.data);
saveAs(options: Object|string)
await t.saveAs('./output.docx');