Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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 iterate through multiple records and fields to be merged into in your document.  Examples of this are displaying a list of contacts, or displaying a list of violations associated with an evaluation. It is possible to limit the returned values based on certain criteria  Loops may also be filtered to limit the merge to selected records.  An example of this is filtering a contact loop to only merge contacts that have a particular affiliation type/role code.

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:

.

<<foreach [ permit in permits.OrderBy(e => e.permitNum)]>><</foreach>>

In the above example a sort is added to the loop to display the records in permit number order.

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]>>
<</foreach>>Suffix

Prefix Item1
Item2
Item3
Suffix

Prefix
<<foreach [permit in permits]>><<[Item]>>
<</foreach>>Suffix

Prefix
Item1
Item2
Item3
Suffix

1. <<foreach [permit in permits]>><<[Item]>>
<</foreach>>

1. Item1
2. Item2
3. Item3

<<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"):

<<foreach [contact in contacts.Where(x => x.affiliationTypeCode == “MNR_HAUL”)]>><<[contact.contactName]>><</foreach>>

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:

<<foreach [contact in contacts.Where(x => x.affiliationTypeCode == “MNR_HAUL”).Take(1)]>><<[contact.contactName]>><</foreach>>

To return a different contact role when another role record does not exist, syntax such as the following can be used:

<<foreach [contact in contacts.Where(x => x.affiliationTypeCode == “ENGR”).Take(1)]>><<[contact.contactLastName]>><</foreach>><<if[contacts.Where(x => x.affiliationTypeCode == “ENGR”).Count() == 0]>><<foreach [contact in contacts.Where(x => x.affiliationTypeCode == “OWNR”).Take(1)]>><<[contact.contactLastName]>><</foreach>><</if>>

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:

  1. Create a variable and set it to "true". 

  2. At the beginning of the loop, check to see that the variable is equal to "true".

  3. 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.)

<<var [keepGoing = “true”]>>
<<if[violations!=null]>>
  <<foreach [violation in violations]>>
    <<if [keepGoing==”true”]>>
      <<if [regulationReference !=null]>>
        <<if [regulationReference.StartsWith(“W50-51”)]>>
          W50-51 Failure to meet the applicable requirements of the Windsor Solutions Standards 

for 

for Wastewater Facility Construction requirements.
          <<var [keepGoing = “false”]>>
        <</if>>
      <</if>>
    <</if>>
  <</foreach>>
<</if>>

In the above example, the text "W50-51 Failure to meet the applicable requirements..." is displayed if the reference begins with “W50-51”.

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]>>

<</foreach>>

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:

Image Modified

  1. <<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). 

  2. <<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.

  3. Date formatting example. This example shows how to format a date in year-month-day order, e.g., 2019.04.25.

  4. 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

Tip

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

Table of Contents


Sub-Topics

Page Tree
root@self


Related Content