We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dear devs,
Is there any way to access the index inside of a loop? Like AngularJS $index.
Use case:
This data
{ "products": [ "Windows", "Mac OSX", "Ubuntu" ] }
And this template, or something similar
{#products} {$index} - {.} {/products}
To generate this
1 - Windows 2 - Mac OSX 3 - Ubuntu
Of course, I could generate the index along with the data, but I'd like to handle it at template level, if possible, not at data level.
The text was updated successfully, but these errors were encountered:
This answer requires docxtemplater 3.32.3 or later.
You can first install angular-expressions : by running : npm install --save angular-expressions
npm install --save angular-expressions
Then in your code, use the following :
const expressionParser = require("docxtemplater/expressions.js"); new Docxtemplater(zip, { parser: expressionParser });
This will allow tags such as {$index}, {$index === 0} , {$index + 1} , ...
{$index}
{$index === 0}
{$index + 1}
This is possible with version 3.6.2 (released a few minutes ago).
To do it , use following code :
function parser(tag) { return { get(scope, context) { if (tag === "$index") { const indexes = context.scopePathItem; return indexes[indexes.length - 1]; } return scope[tag]; }, }; } const doc = new Docxtemplater(zip, {parser}); doc.setData({}); doc.render(); const buffer = doc.getZip();
See https://docxtemplater.readthedocs.io/en/latest/configuration.html#custom-parser for full documentation.
However, with this solution, things such as {$index + 1} or other calcutions don't work.
Sorry, something went wrong.
No branches or pull requests
Dear devs,
Is there any way to access the index inside of a loop? Like AngularJS $index.
Use case:
This data
And this template, or something similar
To generate this
Of course, I could generate the index along with the data, but I'd like to handle it at template level, if possible, not at data level.
The text was updated successfully, but these errors were encountered: