This #foreach loop will result in a $ allProducts list (object) to query all product $ products (target) over again. Each time it is loop, the value obtained from $ allProducts will be placed in the $ product variable.
The content of the $ allProducts variable is a vector, a hash table or an array. The value assigned to the $ produter variable is a Java object and can be referenced from a similar variable. For example, if $ product is really a Java product class, its name can be retrieved by reference to the product.name method (ie: $ product.getname ()).
We assume that $ allProducts is a hash table. If you want to retrieve the value of the key or in the hash table, you can use the following code:
Velocity provides a easier way or loop count so you can do the following:
#foreach ($ Customer IN $ Customerlist)
$ VelocityCount TD>
$ Customer.Name TD> TR>
#end
table>
The default name of the loop count variable is $ VELOCITYCOUNT, labeled in the Velocity.properties configuration file. By default, the variable is counted from 1, but can be set from 0 or 1 in the velocity.properties file. Below is a section of the cyclic variable in the velocity.properties file:
# Default name of the loop counter
# Variable Reference.
Directive.Foreach.counter.name = velocitycount
# Default Starting Value of The Loop
# counter variable reference.
Directive.Foreach.counter.initial.Value = 1
2. Contains
#include scripting element allows template designers to include (import) local files, this file will insert into the #include instruction defined place. The content of the file is not rendered by a template engine. Causes are safe, the files being included can only be placed under Template_Root.
#include ("one.txt")
#include instructions referenced in double quotes. If more than one file is used, it is separated from comma.
#include ("One.gif", "Two.txt", "Three.htm")
The files that are included are not necessarily referenced by file names. In fact, the best way is to use variables instead of file names. This determines the target output is useful when deciding when to submit a page based on the rule.
#include ("Greetings.txt", $ SeasonalStock)
3. Resolution #Parse script elements Allow the page designer to import local files that contain VTL. Velocity will resolve and render the specified template.
#parse ("me.vm")
Just like #include instructions, # parse can use variables instead of a real template file. #parse referenced template file must contain the Template_Root specified by the directory. And the #include directive is different, #parse has only one parameter.
VTL template templates can have #parse statements referring to templates that in turn have #parse statements. By default set to 10, the parse_directive.maxdepth line of the velocity.properties allows users to customize maximum number of #parse referrals that can occur from a single template. (Note: If the parse_directive.maxdepth property is absent from the velocity.properties file, Velocity will set this default to 10.) Recursion is permitted, for example, if the template dofoo.vm contains the following lines:
Count down.
#set ($ count = 8)
#PARSE ("Parsefoo.VM")
All done with dofoo.vm!
IT Would Reference The Template Parsefoo.VM, Which Might Contain The Following VTL:
$ count
#set ($ count = $ count - 1)
#if ($ count> 0)
#PARSE ("Parsefoo.VM")
#ELSE
All done with parsefoo.vm!
#end
After "Count down." Is displayed, Velocity passes through parsefoo.vm, counting down from 8. When the count reaches 0, it will display the "All done with parsefoo.vm!" Message. At this point, Velocity will return to Dofoo.vm and output the "all done with dofoo.vm!" message.
4. Stop
#stop script allows the template designer to stop the implementation of the template engine and return. This is usually used as debugging.