We’ve been happily using the Documents REST API for some time now. We have decided to test your new Modern docx engine to see if we should start using it instead of the classic one, and we are facing an issue that stops us from using it. I wanted to know if we are doing something wrong or what changes we have to do so it work on the new engine.
Dear {{Recipients.Name}},
Here are the details of your application:
Reference: {{Record.Reference}}
In the Classic engine, the template generates a word document with 2 pages, 1 for each recipient, and the Reference of the application is set in both instances correctly.
In the Modern engine, the {{Record.Reference}} formula is not properly resolved and returns an empty string instead of the actual reference number.
How can we achieve the same functionality in the new engine?
Since Record.Reference is not part of the Recipients array, the variable {{Record.Reference}} will not be available inside the {{#each Recipients}} loop when using the Modern engine.
In the Modern engine, you need to explicitly store the value from the upper level (Record) before starting the loop.
Here’s how to adjust your template:
{{reference = Record.Reference}}
Dear {{Recipients.Name}},
Here are the details of your application:
Reference: {{reference}}
This way, the value from Record.Reference is assigned to a separate property (reference), which will be available inside the Recipients loop for each page.
That solution can be quite impractical if the amount of data you want to add inside the loop is considerable, as it would force you to add a lot of explicit variable assignments.
Have you considered allowing referencing top level properties by prefixing the formula?
For example: {{#Record.Reference}} using the # (or $, %, ^ or any special character) as a way to tell the engine that it should pick up the data from the top level data structure and not from the current element from the loop.
Something along those lines would simplify the constructions of the templates as well as the transition between classic engine templates to modern ones.
Today we released an update that fixes this behavior.
You can now use this template in the Modern engine; it will work the same way as in Classic.
Please give it a try:
Dear {{Recipients.Name}},
Here are the details of your application:
Reference: {{Record.Reference}}