You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by newbie-gero <ch...@e7tech.com> on 2008/06/03 10:14:31 UTC

How to loop a for loop in velocity with counters

Greetings, i like to loop 2 for loops to display out the data.

For example in methodList, it contains 
element 0 : register mail 
element 1 : Normal mail

In paymentList, it contains
element 0: $5
element 1: $2


Currently this is what i have code in velocity:

#foreach( $code in $methodList)  $code.method :#foreach( $charge in
$paymentList) $charge.delivery_charge #end #end 

The results display are
REGISTER_MAIL : S$5.00  S$1.00    NORMAL_MAIL : S$5.00 
S$1.00  

Actually i want it to display as
REGISTER_MAIL : S$5.00
NORMAL_MAIL : S$1.00 

How do i do it in velocity?

Thanks in advance


-- 
View this message in context: http://www.nabble.com/How-to-loop-a-for-loop-in-velocity-with-counters-tp17618412p17618412.html
Sent from the Velocity - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: How to loop a for loop in velocity with counters

Posted by ap...@recks.org.
If the default is
   directive.foreach.counter.initial.value = 1
Then you are almost there... You should put your #set directive inside the loop:

#foreach( $code in $methodList)
#set($Count = $velocityCount - 1)
$code.method: $paymentList.get($Count)
#end

Or to be independent of the global setting:

#set($count = 0)
#foreach( $code in $methodList)
$code.method: $paymentList.get($count)
#set($count = $count + 1)
#end


> Please let me know what have i gone wrong

What did you get?
We cannot guess what you got differs to what you expected...

Cheers,
Christoph

newbie-gero wrote:
> Sorry this is not duplicate message. What i mean at the previous messge is
> this of what i have code
> 
> #set($Count = $velocityCount -1)
> 
> #foreach( $code in $methodList)
> $code.method: $paymentList.get($Count)
> #end
> 
> I do this so that the counter will start from zero instead of 1.
> 
> Please let me know what have i gone wrong
> 
> 
> 
> newbie-gero wrote:
>> Hi thanks for the prompt reply.
>>
>> I have try the method you have suggest but i encounter this error
>> org.apache.velocity.exception.MethodInvocationException: Invocation of
>> method 'get' in  class java.util.ArrayList threw exception
>> java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
>>
>> I tried using this #set($velocityCount = $velocityCount -1) but it
>> encounters another error?
>>
>> Any idea how to solve?
>>
>> Thanks
>>
>>
>> apache-10 wrote:
>>> Hi, you should be able to do:
>>>
>>> #foreach( $code in $methodList)
>>>    $code.method: $paymentList.get($velocityCount)
>>> #end
>>>
>>> Cheers,
>>> Chirstoph
>>>
>>> newbie-gero wrote:
>>>> Greetings, i like to loop 2 for loops to display out the data.
>>>>
>>>> For example in methodList, it contains 
>>>> element 0 : register mail 
>>>> element 1 : Normal mail
>>>>
>>>> In paymentList, it contains
>>>> element 0: $5
>>>> element 1: $2
>>>>
>>>>
>>>> Currently this is what i have code in velocity:
>>>>
>>>> #foreach( $code in $methodList)  $code.method :#foreach( $charge in
>>>> $paymentList) $charge.delivery_charge #end #end 
>>>>
>>>> The results display are
>>>> REGISTER_MAIL : S$5.00  S$1.00    NORMAL_MAIL : S$5.00 
>>>> S$1.00  
>>>>
>>>> Actually i want it to display as
>>>> REGISTER_MAIL : S$5.00
>>>> NORMAL_MAIL : S$1.00 
>>>>
>>>> How do i do it in velocity?
>>>>
>>>> Thanks in advance
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>>> For additional commands, e-mail: user-help@velocity.apache.org
>>>
>>>
>>>
>>
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: How to loop a for loop in velocity with counters

Posted by newbie-gero <ch...@e7tech.com>.
Sorry this is not duplicate message. What i mean at the previous messge is
this of what i have code

#set($Count = $velocityCount -1)

#foreach( $code in $methodList)
$code.method: $paymentList.get($Count)
#end

I do this so that the counter will start from zero instead of 1.

Please let me know what have i gone wrong



newbie-gero wrote:
> 
> Hi thanks for the prompt reply.
> 
> I have try the method you have suggest but i encounter this error
> org.apache.velocity.exception.MethodInvocationException: Invocation of
> method 'get' in  class java.util.ArrayList threw exception
> java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
> 
> I tried using this #set($velocityCount = $velocityCount -1) but it
> encounters another error?
> 
> Any idea how to solve?
> 
> Thanks
> 
> 
> apache-10 wrote:
>> 
>> Hi, you should be able to do:
>> 
>> #foreach( $code in $methodList)
>>    $code.method: $paymentList.get($velocityCount)
>> #end
>> 
>> Cheers,
>> Chirstoph
>> 
>> newbie-gero wrote:
>>> Greetings, i like to loop 2 for loops to display out the data.
>>> 
>>> For example in methodList, it contains 
>>> element 0 : register mail 
>>> element 1 : Normal mail
>>> 
>>> In paymentList, it contains
>>> element 0: $5
>>> element 1: $2
>>> 
>>> 
>>> Currently this is what i have code in velocity:
>>> 
>>> #foreach( $code in $methodList)  $code.method :#foreach( $charge in
>>> $paymentList) $charge.delivery_charge #end #end 
>>> 
>>> The results display are
>>> REGISTER_MAIL : S$5.00  S$1.00    NORMAL_MAIL : S$5.00 
>>> S$1.00  
>>> 
>>> Actually i want it to display as
>>> REGISTER_MAIL : S$5.00
>>> NORMAL_MAIL : S$1.00 
>>> 
>>> How do i do it in velocity?
>>> 
>>> Thanks in advance
>>> 
>>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-loop-a-for-loop-in-velocity-with-counters-tp17618412p17619597.html
Sent from the Velocity - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: How to loop a for loop in velocity with counters

Posted by ap...@recks.org.
See Claude's note on this:
:) Christoph
-------- Original Message --------
Subject: Re: How to loop a for loop in velocity with counters
Date: Tue, 03 Jun 2008 11:33:00 +0200
From: Claude Brisson <cl...@renegat.net>

If it doesn't break other templates, you can try setting:

	directive.foreach.counter.initial.value = 0

in your velocity.properties file.

   Claude

newbie-gero wrote:
> Hi thanks for the prompt reply.
> 
> I have try the method you have suggest but i encounter this error
> org.apache.velocity.exception.MethodInvocationException: Invocation of
> method 'get' in  class java.util.ArrayList threw exception
> java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
> 
> I tried using this #set($velocityCount = $velocityCount -1) but it
> encounters another error?
> 
> Any idea how to solve?
> 
> Thanks
> 
> 
> apache-10 wrote:
>> Hi, you should be able to do:
>>
>> #foreach( $code in $methodList)
>>    $code.method: $paymentList.get($velocityCount)
>> #end
>>
>> Cheers,
>> Chirstoph
>>
>> newbie-gero wrote:
>>> Greetings, i like to loop 2 for loops to display out the data.
>>>
>>> For example in methodList, it contains 
>>> element 0 : register mail 
>>> element 1 : Normal mail
>>>
>>> In paymentList, it contains
>>> element 0: $5
>>> element 1: $2
>>>
>>>
>>> Currently this is what i have code in velocity:
>>>
>>> #foreach( $code in $methodList)  $code.method :#foreach( $charge in
>>> $paymentList) $charge.delivery_charge #end #end 
>>>
>>> The results display are
>>> REGISTER_MAIL : S$5.00  S$1.00    NORMAL_MAIL : S$5.00 
>>> S$1.00  
>>>
>>> Actually i want it to display as
>>> REGISTER_MAIL : S$5.00
>>> NORMAL_MAIL : S$1.00 
>>>
>>> How do i do it in velocity?
>>>
>>> Thanks in advance
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>>
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: How to loop a for loop in velocity with counters

Posted by Claude Brisson <cl...@renegat.net>.
If it doesn't break other templates, you can try setting:

	directive.foreach.counter.initial.value = 0

in your velocity.properties file.

  Claude

Le mardi 03 juin 2008 à 02:13 -0700, newbie-gero a écrit :
> Hi thanks for the prompt reply.
> 
> I have try the method you have suggest but i encounter this error
> org.apache.velocity.exception.MethodInvocationException: Invocation of
> method 'get' in  class java.util.ArrayList threw exception
> java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
> 
> I tried using this #set($velocityCount = $velocityCount -1) but it
> encounters another error?
> 
> Any idea how to solve?
> 
> Thanks
> 
> 
> apache-10 wrote:
> > 
> > Hi, you should be able to do:
> > 
> > #foreach( $code in $methodList)
> >    $code.method: $paymentList.get($velocityCount)
> > #end
> > 
> > Cheers,
> > Chirstoph
> > 
> > newbie-gero wrote:
> >> Greetings, i like to loop 2 for loops to display out the data.
> >> 
> >> For example in methodList, it contains 
> >> element 0 : register mail 
> >> element 1 : Normal mail
> >> 
> >> In paymentList, it contains
> >> element 0: $5
> >> element 1: $2
> >> 
> >> 
> >> Currently this is what i have code in velocity:
> >> 
> >> #foreach( $code in $methodList)  $code.method :#foreach( $charge in
> >> $paymentList) $charge.delivery_charge #end #end 
> >> 
> >> The results display are
> >> REGISTER_MAIL : S$5.00  S$1.00    NORMAL_MAIL : S$5.00 
> >> S$1.00  
> >> 
> >> Actually i want it to display as
> >> REGISTER_MAIL : S$5.00
> >> NORMAL_MAIL : S$1.00 
> >> 
> >> How do i do it in velocity?
> >> 
> >> Thanks in advance
> >> 
> >> 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> > For additional commands, e-mail: user-help@velocity.apache.org
> > 
> > 
> > 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: How to loop a for loop in velocity with counters

Posted by newbie-gero <ch...@e7tech.com>.
Hi thanks for the prompt reply.

I have try the method you have suggest but i encounter this error
org.apache.velocity.exception.MethodInvocationException: Invocation of
method 'get' in  class java.util.ArrayList threw exception
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2

I tried using this #set($velocityCount = $velocityCount -1) but it
encounters another error?

Any idea how to solve?

Thanks


apache-10 wrote:
> 
> Hi, you should be able to do:
> 
> #foreach( $code in $methodList)
>    $code.method: $paymentList.get($velocityCount)
> #end
> 
> Cheers,
> Chirstoph
> 
> newbie-gero wrote:
>> Greetings, i like to loop 2 for loops to display out the data.
>> 
>> For example in methodList, it contains 
>> element 0 : register mail 
>> element 1 : Normal mail
>> 
>> In paymentList, it contains
>> element 0: $5
>> element 1: $2
>> 
>> 
>> Currently this is what i have code in velocity:
>> 
>> #foreach( $code in $methodList)  $code.method :#foreach( $charge in
>> $paymentList) $charge.delivery_charge #end #end 
>> 
>> The results display are
>> REGISTER_MAIL : S$5.00  S$1.00    NORMAL_MAIL : S$5.00 
>> S$1.00  
>> 
>> Actually i want it to display as
>> REGISTER_MAIL : S$5.00
>> NORMAL_MAIL : S$1.00 
>> 
>> How do i do it in velocity?
>> 
>> Thanks in advance
>> 
>> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-loop-a-for-loop-in-velocity-with-counters-tp17618412p17619376.html
Sent from the Velocity - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: How to loop a for loop in velocity with counters

Posted by ap...@recks.org.
Hi, you should be able to do:

#foreach( $code in $methodList)
   $code.method: $paymentList.get($velocityCount)
#end

Cheers,
Chirstoph

newbie-gero wrote:
> Greetings, i like to loop 2 for loops to display out the data.
> 
> For example in methodList, it contains 
> element 0 : register mail 
> element 1 : Normal mail
> 
> In paymentList, it contains
> element 0: $5
> element 1: $2
> 
> 
> Currently this is what i have code in velocity:
> 
> #foreach( $code in $methodList)  $code.method :#foreach( $charge in
> $paymentList) $charge.delivery_charge #end #end 
> 
> The results display are
> REGISTER_MAIL : S$5.00  S$1.00    NORMAL_MAIL : S$5.00 
> S$1.00  
> 
> Actually i want it to display as
> REGISTER_MAIL : S$5.00
> NORMAL_MAIL : S$1.00 
> 
> How do i do it in velocity?
> 
> Thanks in advance
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org