Similarly to the article covering Numbered and Bulleted Lists; the Encodian population engine supports repeating sections/paragraphs/tables, in fact, anything within a word document that could be repeated. The source JSON data must be in the form of an array, i.e. one or more items in the list.
- Populate repeating text / paragraphs
- Populate repeating Tables
- Populate repeating Images
- Nested repeating sections
- Checking for Null Arrays
Populate a repeating sections / paragraphs
Example Document: Encodian - Template Syntax - Repeats.docx
Example JSON Data:
{
"companyName": "Encodian",
"members": [
{
"listFirstName": "Efren",
"listLastName": "Gaskill",
"age": 45,
"homeTown": "Clearwater"
},
{
"listFirstName": "Sanly",
"listLastName": "Keyme",
"age": 19,
"homeTown": "Kensington"
},
{
"listFirstName": "Mark",
"listLastName": "Nigma",
"age": 36,
"homeTown": "Silver Spring"
}
]
}
Configure the document as required interlacing tokens within your actual content, for example:
<<foreach [member in members]>><<[member.listFirstName]>> <<[member.listLastName]>> is <<[member.age]>> years old and lives in <<[member.homeTown]>>.
<</foreach>>
This generates the following output:
Populate repeating Tables
Please see this blog article for an example of how to repeat a table within a document. The example document template is provided in the blog.
Populate repeating Images
Resources with examples:
- This blog article contains a template file and an example of how to repeat images within a document
- This video from 13:00 min
Example JSON Data:
{
"companyName": "Encodian",
"Attachments": [
{
"imageBase64": "Base 64 encoded string"
},
{
"imageBase64": "Base 64 encoded string"
}
]
}
Configure the document by wrapping the image token text box with the opening and closing for each tags. Word defaults to the Layout Option "Text wrapping" which moves the text box out of the for each tags therefore select "In Line with Text" within the Layout Options:
Nested repeating sections
You can include any number of repeating sections within repeating sections to populate nested arrays. Repeating sections can contain any data that the template syntax supports e.g. images, tables, plain text, etc.
Consider a list of companies with each company containing a list of employees:
Example Document: Encodian - Template Syntax - Nested arrays.docx
Example JSON Data:
{
"items": [
{
"Company": "Encodian",
"Employees": [
{
"Name": "Sarah Goodwin",
"Title": "Managing Director",
},
{
"Name": "John Smith",
"Title": "Operations Director",
}
]
},
{
"Company": "Microsoft",
"Employees": [
{
"Name": "Alex Li",
"Title": "Managing Director",
},
{
"Name": "Barbara Myers ",
"Title": "Technical Director",
}
]
}
]
}
Configure the template with a repeating section within a repeating section:
This generates the following output:
Enabling null arrays/members
By default the 'Populate Word Document' actions 'Allow Missing Values' property is set to 'No' which will throw an error if a member has a null value, i.e. the following JSON will generate the following error:
{
"companyName": "Encodian",
"members": []
}
The complete error message: A general error occurred populating the word document provided: An error has been encountered at the end of expression 'member.listFirstName]>'. Can not get the value of member 'listFirstName' on type 'System.Data.DataRow'.
If elements of your JSON data could be null, then simply change the 'Allow Missing Values' property to 'Yes'
Checking for Null Arrays
The Any() method can be utilised to check whether an array member contains items or is null.
Example Document: Encodian - Template Syntax - Repeats - Null Array.docx
Example JSON Data:
{
"companyName": "Encodian",
"members": []
}
Configure the document as required using the Any() method to evaluate the array member, for example:
<<if [members.Any()]>>
<<foreach [member in members]>><<[member.listFirstName]>> <<[member.listLastName]>> is <<[member.age]>> years old and lives in <<[member.homeTown]>>.
<</foreach>>
<< else >>
no items
<< /if >>
This generates the following output:
0 Comments