guigrpa / docx-templates

Template-based docx report creation
MIT License
883 stars 145 forks source link

Loop with sections and dynamic headers #310

Closed philip-roeggla closed 1 year ago

philip-roeggla commented 1 year ago

I did not find the solution in the docs, maybe you can help me: I would like to loop through data, generating sections for each datum, with a header set from that datum. For example

[Ms-Word-Header]
+++FOR datum in data+++
+++INS $datum.headerText+++
[Ms-Word-Body]
+++INS $datum.content+++
[Ms-Word-SectionBreak]
+++END-FOR datum+++

Which does not work this way, since it seems, you can't have template code parted over header and body. I tried to define the loop only in the body, and reference the data later on in the header, but then it does not find it.

Is there a way to do this?

Thanks! And als thanks for the great library!

jjhbw commented 1 year ago

I'm afraid that's currently not possible. The document's headers and its body are contained in separate XML trees in the docx file and are processed separately. This makes it difficult to implement something like you describe.

philip-roeggla commented 1 year ago

Ok, thanks!