easy-template-x-data-binding
Extends the easy-template-x to implement data binding controls via custom XML.
Easy Template X Extensions
Easy Template X is an excellent library for creating word documents from templates.
It handles the processing of the documents contents but does not allow editing of the document's metadata, held in Custom XML files inside the document.
Easy Template X supports extensions which can be run on the documents either before or after it modifies the contents.
Node Example
;; // 1. read template file; // 2. process the template; ; ; // 3. save outputfs.writeFileSync"out/data binding - output.docx", doc;
Input:
Output:
Browser Example
The following example produces the same output while running in the browser. Notice that the actual template processing (step 2) is exactly the same as in the previous Node example.
; // 1. read template file // (in this example we're loading the template by performing// an AJAX call using the fetch API, another common way to// get your hand on a Blob is to use an HTML File Input);; // 2. process the template; ; ; // 3. save outputsaveFile"myTemplate - output.docx", doc;
Live Demo
Checkout this live demo on CodeSandbox 😎
OOXML Data structure
The data is stored in the /CustomXml/itemX.xml in the OOXML document.
Each piece of bound data can be uniquely identified by the XML path from its root.
Use the XML path to the XML tag used to store the data as its identifier.
Currently namespaces and duplicate tag names are not supported.
"rootnode/firstnode": _type: type value: value "rootnode/nextnode": _type: type value: value "rootnode/lastnode": _type: type value: value
For an example of how to format the data see [test\integration\templateHandler.tests.ts]
The _type dictates which plugin will be used.
The plugin dictates what type the value will have.
Standard plugins
Although internally in the custom XML files all values are stored as text, and the text plugin can be used for all data types, to assist in validating data different plugins are available.
Please feel free to implement plugins for other data types.
Text plugin
Can be used with all content controls. Updates the text of the node with its contents. Expects value to be a string.
Boolean plugin
Mainly used with check box controls. Updates the text of the node with 'true' or 'false'. Expects value to be a boolean.
Date plugin
Mainly used with date box controls. Updates the text of the node with the date in 'yyyy-mm-dd' format. Expects value to be a Date.