ForEach Droplet
This droplet takes a request parameter called "array" which can contain a plain old array or any object whose class implements the Collection interface. The elements of the array can be any Object and are not restricted to primitive or String type. You can even pass a list of Objects of your custom class, and ForEach droplet will iterate it for you.
Usage:
Input Request parameters: array, sortProperties (both optional)
Droplet's output parameters (called OPEN PARAMETERS): outputStart, outputEnd, output, empty
1. ForEach droplet called.
2. A request parameter named "array" (which this droplet expects), is set with the value of a component's (Employee) property ("cars"), which is supposed to be an array (or a collection or list or something..)
3. Droplet services its output parameters (called open parameters), all of which are optional. "outputStart" appears once through "n" number of iterations. As we saw earlier, whenever an "open parameter" (droplet's output parameter) is serviced, the contents inside the <dsp:oparam> tags are printed. Therefore, in our case "The employee has following cars" will be printed once for "n" iterations
4. "output" open parameter is serviced, in which we print a request parameter "element" which is set by our ForEach droplet.
NOTE: We can also change the name by which each element parameter is accessed (i.e. we can replace the "element" variable by any other name) using the following tag:-
<dsp:setvalue param="myElement" paramvalue="element" />
Now, you can use <dsp:valueof param="myElement"></dsp:valueof>
This step prints the value of each element of the array passed. (because we are just printing the element value inside <dsp:oparam> tags)
5. "outputEnd" parameter, just like "outputStart" is serviced once for "n" iterations. Contents inside <dsp:oparam> tags will be printed as-is.
Now, let us see how the output would shape up step-by-step:
1. This just calls the droplet, so no output.
2. Let us assume that Employee component's cars property contains the following: ["Honda", "BMW", "Audi"]. This array is passed to the droplet.
3. Contents inside <dsp:oparam> tags for open parameter "outputStart" will be printed once:-
The employee has following cars
4. Each element present in "cars" property will be printed in <li> tags
- Honda
- BMW
- Audi
5. Finally, when all the parameters have been printed, "outputEnd" open parameter is serviced, and contents inside <dsp:oparam> tags of outputEnd tags will be printed once.
Thats All!!
Therefore, the final output will be something like:-
The employee has following cars
Now that we understood how ForEach droplet functions, we can also have a look at other important droplets present in ATG documentation:-
1. ForEach : Traverses an array/collection/list.
2. IsEmpty : To check whether the passed parameter is empty or not.
3. IsNull : To check whether the passed parameter is null or not.
4. ItemLookupDroplet : To lookup items in the repository (Please go through the repository articles first before reading this)
5. ProtocolChange : To switch between http and https.
6. Redirect : To redirect to some other page.
7. Switch : Displays one of sevaral possible values depending on input.
8. List of all other droplets
Now that we have seen some weird JSP syntax.. its time to understand that..!
Let us move on and understand ATG's dsp tag library and some useful syntax.
I think there's an error in the example output. You have written the "That's all!" string as a list element. Great guide by the way, thanks!
ReplyDeleteThat is intentional :)
DeleteThe output will print as list item for "That's all" too?
DeleteNo.
Delete