Closed antipopp closed 2 years ago
I'm not sure what you mean. Just expose the array to the renderer using the data
property like here https://github.com/guigrpa/docx-templates/blob/43260eb207744f40de9d9acf27c222f5a98ce5df/src/__tests__/templating.test.ts#L121
In the above example, the 'companies' variable now holds the array and you can refer to it as if it were a simple variable in the template's scope.
Let me know if I misunderstood your question.
It's a solution (and it's the one I'm currently using), but I was thinking to pass just the array to the data field (data: [...]
), so I was wondering if there was some "data" alias/keyword to access the array.
This is because I'm building the JSON on a Go backend, where you generate the JSON by just building an array of objects with no parent prop. To add the "companies" prop I must make throwaway struct (object) to just name it.
That is not possible; the values of the data
object are injected as globals in the VM scope when rendering the template, so they can be referenced from the JS in the template. You can't expose an unwrapped array like this; how would you reference its elements from the template scope? Every top-level global needs a name.
The solution you're currently using is the only supported way. Hope this helped.
How can you loop a JSON array without a parent object? Something like:
What should I use in the template?