Skip to main content
Windward

ForEach Tag Basics

Overview

The ForEach is one of the most powerful tags in AutoTag. This is the tag that lets you created thousands of rows from a single row of other tags. This article will explain common uses and common trouble-spots of using the ForEach Tag.

 

ForEach Loops and Your Data

The Basics

  • The ForEach and EndForEach tags must be used together. Windward calls a single set of ForEach/EndForEach Tags (and all the content in between) a "ForEach Loop."
    • ForEach Loops can be nested. See below for more details.

 

  • A ForEach tag does not print anything on its own -- its only purpose is to define a set of data that you want to make available to the tags that follow. (It also does some nifty formatting things, but that's in the next section).
    • If you try to use a ForEach Tag without an Out Tag, it will seem like nothing has printed at all in your output report. 
    • If you try to set up a Function (like an If statement, a SUM, etc.), you'll get an error, or the looped Out Tags will have nothing to print. 

Selecting Data for your Out Tags

  • When you set up a ForEach Loop, you first select the data you want to be available to the tags that follow. The Tag Editor will automatically assign a variable name (the default is ${varName1}, but you can change it in the Properties pane of the Tag Editor).  
    • When you select the data for the tags inside the Loop, you must choose from the expandable tree of data that is associated with your variable name for the ForEach Loop, and NOT from the main tree of your Data. See below.

 

Selecting Data for Nested Loops

  • When you create nested ForEach Loops, the data for the Inner Loop(yellow) must be part of the data that you already defined in the Outer Loop (green). This is true no matter how many nested loops you have.
  • Subsequent Nested Loops (orange) will also be limited to what was already defined.

 

 

  • If a Nested Loop (yellow) selects more data than the Outer Loop (green), the only data that will be available to the Inner Loop is the data that is already included in the set you defined for the Outer Loop. 
  • Similarly, if your Nested Loop has no common data with the Outer Loop, your Tags will return no data. 

 

  • You can use as many ForEach Loops in a template as you like. The limitations of nested loops only apply to nested loops.
    • In this scenario, the Orange Loop doesn't need to have any data in common with the Green Loop.

 

 

Common Obstacles

Unexpected Results

It's common that after spending time carefully building a ForEach Loop, that you don't get the results you were expecting. Here are some things to keep in mind:

  • When setting filters on your ForEach Tag's data set, be mindful of whether the filtered element appears in all the branches of your data. For example, you might have a table of Orders, but you only want the data for a few specific employees -- you could not use Employees/Employee/FirstName, you would have to use Employees/Employee/EmployeeID.  This sounds straightforward, but most data sources are much more complex than what is shown.
    • If you were expecting more data or different data, get really basic. Recreate your ForEach Loops in a new document, but only include 1 Out Tag at a time to see what is returned. 

 

Preview doesn't match results

It's common to see a Preview result that is different than what is actually Output. Sums may seem to not match. Tables may seem to return more or less data. 

  • When you use the Preview Button in the Tag Editor, Windward is checking that single tag and its logic. When you Output, Windward takes into account all the logic of your whole template -- there may be Tag settings, If/EndIf tags, and Nested Loop filters (to name a few) that affect the data which is actually printed. 
  • Try isolating tags or regions of your template that seem problematic. Recreate your ForEach Loops in a new document, but only include 1 Out Tag at a time to see what is returned. This may help you see where you need to make a change.