Skip to content
New issue

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

Writing conditions in loop for an array of primitive data like string #597

Closed
shubham93 opened this issue Mar 30, 2021 · 2 comments
Closed

Comments

@shubham93
Copy link

shubham93 commented Mar 30, 2021

Hi,

Current version of docxtemplater: 3.24.1
I have a usecase where I have an array of numbers such as

data = [1, 3, 5]

I have to write conditions based on both the index and the current loop value inside the loop

As per the issue #397, I believe we should be able to use the $index to write conditions for index. I tried the below code for index and it did not work on the demo site.

{#data}

{#$index === 0}
Section A
{/}
{#$index === 1}
Section B
{/}

{/} 


As per the issue #535, it is explained that we can write conditions on the docx for non primitive data types inside array.


But if it is just an array of number like the above shared data how should we go about it?


I tried to write it in the following manner:

{#data}

{#. == 1}
Section A
{/}

{/}

Error given: The scope parser for the tag ". == 0" failed to compile

Actual docx :
Actual docx.docx

Generated docx:
generated (9).docx

@edi9999
Copy link
Member

edi9999 commented Apr 1, 2021

Hello @shubham93,

I have just added a new section in the doc about this :

You could use the following https://docxtemplater.readthedocs.io/en/latest/angular_parse.html#retrieving-index-as-part-of-an-expression

Hope, that helps,

Edgar

@edi9999
Copy link
Member

edi9999 commented Nov 22, 2022

This can now be done using the following instead :

This answer requires docxtemplater 3.32.3 or later.

You can first install angular-expressions : by running : 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} , ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants