You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Tomasz Mielnik <to...@tdcinternet.pl> on 2003/01/29 09:04:14 UTC
selecting from foreach loop
hello
i'm developing a webapp where i found the following problem:
i have foreach loop:
#foreach($log in $history)
HTML HERE
#end
where $history is a java.util.Vector object.
my question is: how to make a loop in velocity which will behave like
for loop for example:
for (i=5; i<10; i++);
in other word how to select not all fields from vector but only a
specified range of them?
sorry if this subject was discused before, but i cant find it.
Tomasz Mielnik/Tdc Internet Polska
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: selecting from foreach loop
Posted by Ch...@dlr.de.
you can use the $velocityCount as others already replied in
this thread or you can use the range operator and the
java.util.Vector.get() method:
#for( $i in [5..10] )
$history.get($i)
#end
I guess you want to display the last N lines, so you can try
to do (this is untested code out of my mind):
#set( $count = $history.size() )
#set( $from = $count - 10 )
#for( $i in [${from}..${count}] )
$history.get($i)
#end
Velocity is cool! Hope this tip helped you on...
--
:) Christoph Reck
Tomasz Mielnik wrote:
> hello
> i'm developing a webapp where i found the following problem:
> i have foreach loop:
>
> #foreach($log in $history)
> HTML HERE
> #end
>
> where $history is a java.util.Vector object.
>
> my question is: how to make a loop in velocity which will behave like
> for loop for example:
> for (i=5; i<10; i++);
> in other word how to select not all fields from vector but only a
> specified range of them?
> sorry if this subject was discused before, but i cant find it.
>
> Tomasz Mielnik/Tdc Internet Polska
--
:) Christoph Reck
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: selecting from foreach loop
Posted by Tomasz Mielnik <to...@tdcinternet.pl>.
W liście z śro, 29-01-2003, godz. 09:18, Derick Fernando pisze:
> Hello,
>
> It would be best to limit the number of objects in your java class itself.
> But if you feel there is no other way, you could do something like:
>
> in your Velocity.props 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
>
> then you could do something like
> #foreach($log in $history)
> #if( $velocityCount < 10)
> HTML
> #end
> #end
>
thanks for help.
to be honest i did it exactly like this but i was looking for better
solutions and i must say that i like the idea with limiting number of
elements in vector.
thanks again
oh and btw: here is how i did it:
#foreach($log in $history)
#set($counter=$velocityCount+$listcounter)
#if($velocityCount>$listcounter &&
$velocityCount<$listlimit+$listcounter)
HTML HERE
#end
#end
Tomasz Mielnik
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: selecting from foreach loop
Posted by Derick Fernando <de...@xenocex.com>.
Hello,
It would be best to limit the number of objects in your java class itself.
But if you feel there is no other way, you could do something like:
in your Velocity.props 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
then you could do something like
#foreach($log in $history)
#if( $velocityCount < 10)
HTML
#end
#end
( of course you could create a count variable as well, #set( $count = 0 ) )
etc..
Hope that helps,
Derick
----- Original Message -----
From: "Tomasz Mielnik" <to...@tdcinternet.pl>
To: <ve...@jakarta.apache.org>
Sent: Wednesday, January 29, 2003 12:04 AM
Subject: selecting from foreach loop
> hello
> i'm developing a webapp where i found the following problem:
> i have foreach loop:
>
> #foreach($log in $history)
> HTML HERE
> #end
>
> where $history is a java.util.Vector object.
>
> my question is: how to make a loop in velocity which will behave like
> for loop for example:
> for (i=5; i<10; i++);
> in other word how to select not all fields from vector but only a
> specified range of them?
> sorry if this subject was discused before, but i cant find it.
>
> Tomasz Mielnik/Tdc Internet Polska
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: selecting from foreach loop
Posted by Borja Martín <bm...@hola.com>.
try this:
#foreach($i in [3..6])
$history.get($i)
#end
it should work.
byez.
----- Original Message -----
From: "Tomasz Mielnik" <to...@tdcinternet.pl>
To: <ve...@jakarta.apache.org>
Sent: Wednesday, January 29, 2003 9:04 AM
Subject: selecting from foreach loop
> hello
> i'm developing a webapp where i found the following problem:
> i have foreach loop:
>
> #foreach($log in $history)
> HTML HERE
> #end
>
> where $history is a java.util.Vector object.
>
> my question is: how to make a loop in velocity which will behave like
> for loop for example:
> for (i=5; i<10; i++);
> in other word how to select not all fields from vector but only a
> specified range of them?
> sorry if this subject was discused before, but i cant find it.
>
> Tomasz Mielnik/Tdc Internet Polska
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
RE: selecting from foreach loop
Posted by Andy Lee <ag...@earthlink.net>.
At 1:13 PM +0100 1/29/03, Tomasz Mielnik wrote:
>W liÊcie z Êro, 29-01-2003, godz. 10:49, Peter Romianowski pisze:
>
> > #foreach ($pos in [5..9])
> > $history.get($pos)
> > #end
>
One caveat: in loops through integer ranges, Velocity always iterates
at least once. It counts by -1 instead of 1 if the last number is
smaller than the first number. So if $history is empty, the
following innocent-looking loop will generate an
ArrayOutOfBoundsException instead of doing nothing:
#set( $lastIndex = $history.size() - 1 )
#foreach ($pos in [0..$lastIndex])
$history.get($pos)
#end
--Andy
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
RE: selecting from foreach loop
Posted by Tomasz Mielnik <to...@tdcinternet.pl>.
W liście z śro, 29-01-2003, godz. 10:49, Peter Romianowski pisze:
> #foreach ($pos in [5..9])
> $history.get($pos)
> #end
what can i say :)
sweeet. it's simply and it works.
thanks a lot.
Tomasz Mielnik
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
RE: selecting from foreach loop
Posted by Peter Romianowski <me...@gmx.de>.
Tomasz,
> hello
> i'm developing a webapp where i found the following problem:
> i have foreach loop:
>
> #foreach($log in $history)
> HTML HERE
> #end
>
> where $history is a java.util.Vector object.
>
> my question is: how to make a loop in velocity which will
> behave like for loop for example: for (i=5; i<10; i++); in
> other word how to select not all fields from vector but only
> a specified range of them? sorry if this subject was discused
> before, but i cant find it.
#foreach ($pos in [5..9])
$history.get($pos)
#end
is the same as in java: for (pos = 5; pos < 10; pos++)
> Tomasz Mielnik/Tdc Internet Polska
Peter
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org