Search code examples
docxtemplater

docxtemplater and Word Doc headers - how to repeat


I have a Word document that I'm using as a template. I have tags in the header as well as the body. Ideally I would wrap the entire document in a tag representing the name of the array that holds the json objects to iterate over and produce 1 Word document per array item.

However, if I try to wrap the entire document in a tag (by putting the opening tag in the header and the closing tag at the end of the body), then docxtemplater complains about the tag not being closed in the header, and not being opened in the body. If I close the tag in the header and open it in the body, then docxtemplater renders a single Word document with all of the headers mashed into a single header, and all of the bodies mashed into a single body.

Is there a way to have the entire Word document (header and body) repeat a single time per array item?


Solution

  • I fixed my problem with a workaround. I realized that my document only needed a header on the first page, so I put the header content directly in the body of the document. Then I could treat the entire document as one large repeatable element, wrapped with a tag as normal.