docxtemplater
Advanced tools
Changelog
3.52.0
Add syntax.allowUnclosedTag
option.
This allows to write : Hello {user
and not have an error in your template.
Changelog
3.51.2
Improve typescript typings :
DXT.Part
getFileType?(opts: any): string | void;
targets
to Docxtemplater instanceChangelog
3.51.1
Update to be able to write {#loop}{. | filter}{/}
so that the variable passed
to filter is not of type Proxy.
Previously, the variable passed to the filter would be of type Proxy.
This requires angular-expressions@1.4.0
Changelog
3.51.0
Add support for module.preZip
function, which is useful for the subtemplate and the meta module.
After upgrading to 3.51.0, if you use any of the paid modules, please also run the upgrade for all your modules with this command :
npm install docxtemplater && npx -y update-docxtemplater && npm install
Update moduleApiVersion to 3.41.0.
Changelog
3.50.0
In the continuity of the "evaluateIdentifier" feature added in 3.49.0, we added the setIdentifier
option for the expressions.js file :
This is useful if you want to do assignments in your template, like this :
{$$globalVar = 3}
You can then write :
const expressionParser = require("docxtemplater/expressions.js");
const globalData = {};
const doc = new Docxtemplater(zip, {
parser: expressionParser.configure({
setIdentifier(tag, value) {
const matchGlobal = /^\$\$/g;
if (matchGlobal.test(tag)) {
globalData[tag] = value;
return true;
}
},
evaluateIdentifier(tag) {
const matchGlobal = /^\$\$/g;
if (matchGlobal.test(tag)) {
return globalData[tag];
}
},
}),
});
doc.render(/* data */);
In this case, all of your assignments to variable that start with "$$" will be assigned to the "globalData" object.
Also tags that contain one assignment and then a statement will now return the statement.
So for example, you can write :
Hello { $$admin=user; $$admin }
In this case, it will render "Hello John" (if the data is {user: "John"}
)
Changelog
3.49.2
Bugfix corruption that could appear when using the vertical loop module.
Previously, the vertical loop module could sometimes produce empty tables that would not be cleaned.
For example, with following template :
--------------
| {:vt#loop} |
--------------
| XXX |
| {:vt/} |
--------------
If the loop was an empty array, the output would produce a corrupt document.
The table is now correctly removed in this case.
The table will
Changelog
3.49.0
Add possibility, when using the angular parser, to use "magic" keys to return some specific values. (This feature cannot be implemented if you use the "docxtemplater/expressions-ie11.js"
package).
In your template, if you write :
{#loop}
{__val}
{/}
This will retrieve the "val" value from the scope that is above the current scope (it retrieves the value of "val" in the scope outside of the loop).
const expressionParser = require("docxtemplater/expressions.js");
const doc = new Docxtemplater(zip, {
parser: expressionParser.configure({
evaluateIdentifier(tag, scope, scopeList, context) {
const matchesParent = /^(_{2,})(.*)/g;
if (matchesParent.test(tag)) {
const parentCount = tag.replace(matchesParent, "$1").length - 1;
tag = tag.replace(matchesParent, "$2");
if (parentCount >= 1) {
for (let i = scopeList.length - 1 - parentCount; i >= 0; i--) {
const s = scopeList[i];
if (s[tag] != null) {
const property = s[tag];
return typeof property === "function"
? property.bind(s)
: property;
}
}
}
}
},
}),
});
doc.render({
loop: [
{
val: "This value",
},
],
val: "Other value", // <= This value will be retrieved
});
Changelog
3.48.0
Allow to configure the behavior of the "change delimiter syntax".
As documented here :
https://docxtemplater.com/docs/tag-types/#set-delimiter
You can for example use :
{=[[ ]]=}
[[name]]
It is possible to change the special behavior that will catch tags that start with a "=".
It is either possible to set the syntax.changeDelimiterPrefix
to null so that it won't be possible to change the delimiters inside the template, or you can change the char that is used.
For example :
const doc = new Docxtemplater(zip, {
syntax: {
changeDelimiterPrefix: null,
},
});
or
const doc = new Docxtemplater(zip, {
syntax: {
changeDelimiterPrefix: "$",
},
});