velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brede...@mac.com
Subject #foreach tool
Date Tue, 10 Sep 2002 20:42:21 GMT
Guys,

I am writing a little tool that helps to write a #foreach that 
terminates after 10 entries or such things. The source needs to be 
documented but basically the functionality is here.

It can wrap a list, a map, an iterator, a collection, an enumeration.

To get the next element you call $tool.more(). If you don't call 
more(), the tool will terminate the #foreach. That means that you can 
set a condition that will stop the loop very easily.

After that, you can reuse the tool to display the remaining elements. 
You need to do $tool.reset() to start over from the beginning.

Use $tool.hasNext() to check if there are more elements.
I called it FisherMill because it sounds funny, if we put it in the 
tools we could give it a better name :-)

This is an example of use:

Java
----
context.put("mill",new FisherMill());


VTL
---

#set ($list = [1, 2, 3, 5, 8, 13])
#set ($numbers = $mill.wrap($list))

#foreach ($item in $numbers)
#if ($item < 8) $numbers.more()#end
#end

$numbers.more()


$numbers.reset()
#foreach ($item in $numbers)
    $numbers.more() + $numbers.more() = $numbers.more()
#end


Output
------

  1 2 3 5
8


    1 + 2 = 3
    5 + 8 = 13


Cheers,
-- Denis.


Mime
View raw message