- Created by Alecia Elia (Unlicensed) , last modified by Emad Shaat on Feb 14, 2020
You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 5 Next »
Loops can be used to find information pertaining to a contact with a particular affiliation type/role code, it can also be used to return the list of violation associated with an evaluation. It is possible to limit the returned values based on certain criteria.
Loops
A foreach loop will be inserted automatically into the template if a group name (Bold fields name) is double-clicked in the field selection list.
Merge fields set up for a loop can be displayed using a foreach statement such as the example below (in the following loop it will display permits and sort based on permit number:
|
Loop Formatting
The loop above will list all items one after another without any line breaks, to format the foreach loop check the examples below:
Template Code | Output |
---|---|
Prefix<<foreach [permit in permits]>> <<[Item]>><</foreach>>Suffix | Prefix Item1 Item2 Item3 Suffix |
Prefix <<foreach [permit in permits]>><<[Item]>> | Prefix Item1 |
Prefix | Prefix |
1. <<foreach [permit in permits]>><<[Item]>> | 1. Item1 |
<<foreach [item in items]>><<[item.IndexOf() !=0 ? “, “: “”]>><<[item.itemName]>><</foreach>>. | Item1, Item2, Item3. |
Filtered Loops
To filter a loop so that it only displays certain data (such as only displaying a contact with a specific affiliation type), add a ".Where" clause to the foreach syntax. For example, the following code only displays those contacts that have an affiliation type of manure hauler ("MNR_HAUL"):
|
To protect against multiple matching contacts being returned (such as 2 or more manure haulers in the example above), add .Take(1) to the markup:
|
To return a different contact role when another role record does not exist, syntax such as the following can be used:
|
Breaking Out of a Loop
Sometimes it's useful or necessary to break out of a loop when a particular condition has been met or value has been found. This can be achieved by doing the following:
Create a variable and set it to "true".
At the beginning of the loop, check to see that the variable is equal to "true".
Somewhere in the loop, perhaps when a specific condition has been met, set the value of the variable to "false".
The following code illustrates this. (The code is indented here for legibility but doesn't need to be indented in the document template.)
|
Loop in Table
A foreach statement within a table will loop within the table. In the example below, for each feature ID, a new row within the table will be created and the associated fields filled:
Feature ID | Description | Status |
---|---|---|
<<foreach [feature in features.OrderBy(e => e.featureText)]>><<[featureText]>> | <<[featureDescription]>> | <<[status]>> |
Nested Loops
The following example demonstrates a loop for each of a site’s permits and a sub-loop listing each permit’s features in a table:
<<foreach>> syntax example. The foreach tag is used to display the same fields for repeating records. For example, in this figure, the fields between the <<foreach>> and <</foreach>> tags are displayed for all permits associated with this site, regardless of how many permits there are. (Note that the "Site Permits" caption does not repeat for each caption, as it is not included within the <<foreach>> tags. ). Also note the OrderBy syntax. OrderBy should always be used to sort looped data. In this case, the repeated permits are sorted by permit number (permitNum).
<<foreach>> syntax example in a table. In cases in which the foreach tags are used within a table, the template engine is smart enough to know that there should be a row for each looped item.
Date formatting example. This example shows how to format a date in year-month-day order, e.g., 2019.04.25.
Logical if example. This example demonstrates a way of only rendering output for a particular field if data is present in that field. See "Address with check for missing data" below for another example.
Finding Available Affiliation Types
Navigate to Admin > Lookups > Affiliation Types (Roles), this will display existing affiliation types. The Lookup Code is what is used to filter.
Roles with one or more functional areas specified (e.g. Applicant, Permittee) can only be assigned in the designated area(s). Roles that don't have a functional area specified are available in all areas.
Tips and Tricks
- A foreach syntax block can be inserted automatically into the template by double clicking a group name in the field selection list.
On this page
Sub-Topics
Related Content
- No labels