You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Tarun <ta...@secf.com> on 2005/06/22 14:05:19 UTC

How to iterate the two different context in a single foreach loop

hii
I am new to velocity 
i am getting two context on the velocity template say 
context1 and context2. Both these contexts have an array of string and of same length. 
Now in a single loop i want to iterate the values of both the context. But i am not able to do that.
what i was trying to do is that:-
#set($counter = 0)

#foreach($ctx1 in $context1)

<select name="combobox">

<option value="$context2[$counter]">$ctx1</option>

</select>

#end

Here in the foreach loop $ctx1 is returning me the exact values but the $context2[$counter] is returning me the object of the array string.

so i am not able to get the values. So is there any way in which i can iterate the two context in a single foreach loop or any another way you can suggest.

Thanks in Advance

Tarun

Re: How to iterate the two different context in a single foreach loop

Posted by Jason Pettiss <ja...@CatalisHealth.com>.
The typical pattern is when you're augmenting one set of data with 
another. 

You could as you say create another bean which aggregates the two pieces 
of data that you need.  But that's often not appropriate as you don't 
know until generation time what the data is.  A common example is a 
table where the user gets to select which columns appear.  You can 
easily get a query to get each of the columns individually, but it's 
much more difficult to generate a query on the fly which does it all in 
one shot.

The most repeatedly annoying case that comes up is the problem Tarun 
has-- you're trying to generate an HTML SELECT item.  Each OPTION has a 
value and a displayable label.  You've likely already got a hardcoded 
list of allowable values, an enumeration.  The labels typically come up 
as a list of strings from a ResourceBundle.  Now you've got two lists.  
You should be done... but you're not.  Now you have to write or find 
some sort of LabelValueBean and then create N of these objects and throw 
in your two lists-- a big pain and a waste of resources.

The summary gripe is-- if I've already got two or more lists of the same 
size and in the right order, why should I have to define custom objects 
whose only purpose is to aggregate, or worse-- throw one or more of the 
lists into one or more maps, and incur all the penalties of building and 
indexing a map (which I possibly only need for a single request).

Daniel Dekany wrote:

>Thursday, June 23, 2005, 7:43:19 AM, Jason Pettiss wrote:
>
>  
>
>>Do you really mean two different contexts?  Or just two different 
>>lists/collections/arrays, found in your context?  Btw, I've always 
>>thought the ability to do something like:
>>
>>#foreach( $user in $users, $food in $foods )
>>$user likes $food!
>>#end
>>    
>>
>[snip]
>
>Out of curiosity, what are the real-world use-cases for this? Because if
>for users there is a food associated then probably there is a bean for
>each user with a food property, or a Map that maps user name to foods...
>but two lists?
>
>  
>


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


Re: How to iterate the two different context in a single foreach loop

Posted by Daniel Dekany <dd...@freemail.hu>.
Thursday, June 23, 2005, 7:43:19 AM, Jason Pettiss wrote:

> Do you really mean two different contexts?  Or just two different 
> lists/collections/arrays, found in your context?  Btw, I've always 
> thought the ability to do something like:
>
> #foreach( $user in $users, $food in $foods )
> $user likes $food!
> #end
[snip]

Out of curiosity, what are the real-world use-cases for this? Because if
for users there is a food associated then probably there is a bean for
each user with a food property, or a Map that maps user name to foods...
but two lists?

-- 
Best regards,
 Daniel Dekany


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


Re: How to iterate the two different context in a single foreach loop

Posted by Jason Pettiss <ja...@CatalisHealth.com>.
Do you really mean two different contexts?  Or just two different 
lists/collections/arrays, found in your context?  Btw, I've always 
thought the ability to do something like:

#foreach( $user in $users, $food in $foods )
$user likes $food!
#end
 
would be incredibly convenient.  Kinda wished Java 5 would have added 
this ability but instead we just got the same crippled foreach everyone 
else has.  :-)

Anyway, Tarun, you were close with your original workaround:

<option value="$context2[$counter]">$ctx1</option>

except that there's no syntax for array access, so it will just print 
out the full contents of your second list and then follow it with [#] 
each time.  What you want to try instead is:

<option value="$context2.get($counter)">$ctx1</option>

Obviously make sure that $counter is one less than the $velocityCount 
automatic variable or you'll get an index out of bounds exception.

--jason

Tarun wrote:

>Thanks Avinash for replying
>but i am not using the velocity framework. i am using the turbine framework
>with velocity as a view template only. i have stucked in this problem . is
>there any method in which a single foreach loop i can iterate two contexts.
>
>like this #foreach($ctx1 in context1 , $ctx2 in context2) obviously this is
>not working. but like that is there any other way.
>
>----- Original Message ----- 
>From: "avanish" <av...@secf.com>
>To: "Velocity Users List" <ve...@jakarta.apache.org>
>Sent: Thursday, June 23, 2005 8:51 AM
>Subject: Re: How to iterate the two different context in a single foreach
>loop
>
>
>  
>
>>Why r u using two different contexts ?
>>Also this problem can be solved it we do context chaining....
>>
>>----- Original Message -----
>>From: "Tarun" <ta...@secf.com>
>>To: "Velocity Users List" <ve...@jakarta.apache.org>
>>Sent: Wednesday, June 22, 2005 5:35 PM
>>Subject: How to iterate the two different context in a single foreach loop
>>
>>
>>hii
>>I am new to velocity
>>i am getting two context on the velocity template say
>>context1 and context2. Both these contexts have an array of string and of
>>same length.
>>Now in a single loop i want to iterate the values of both the context. But
>>    
>>
>i
>  
>
>>am not able to do that.
>>what i was trying to do is that:-
>>#set($counter = 0)
>>
>>#foreach($ctx1 in $context1)
>>
>><select name="combobox">
>>
>><option value="$context2[$counter]">$ctx1</option>
>>
>></select>
>>
>>#end
>>
>>Here in the foreach loop $ctx1 is returning me the exact values but the
>>$context2[$counter] is returning me the object of the array string.
>>
>>so i am not able to get the values. So is there any way in which i can
>>iterate the two context in a single foreach loop or any another way you
>>    
>>
>can
>  
>
>>suggest.
>>
>>Thanks in Advance
>>
>>Tarun
>>
>>
>>
>>
>>
>>---------------------------------------------------------------------
>>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
>
>  
>


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


Re: How to iterate the two different context in a single foreach loop

Posted by Tarun <ta...@secf.com>.
Thanks Avinash for replying
but i am not using the velocity framework. i am using the turbine framework
with velocity as a view template only. i have stucked in this problem . is
there any method in which a single foreach loop i can iterate two contexts.

like this #foreach($ctx1 in context1 , $ctx2 in context2) obviously this is
not working. but like that is there any other way.

----- Original Message ----- 
From: "avanish" <av...@secf.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Thursday, June 23, 2005 8:51 AM
Subject: Re: How to iterate the two different context in a single foreach
loop


> Why r u using two different contexts ?
> Also this problem can be solved it we do context chaining....
>
> ----- Original Message -----
> From: "Tarun" <ta...@secf.com>
> To: "Velocity Users List" <ve...@jakarta.apache.org>
> Sent: Wednesday, June 22, 2005 5:35 PM
> Subject: How to iterate the two different context in a single foreach loop
>
>
> hii
> I am new to velocity
> i am getting two context on the velocity template say
> context1 and context2. Both these contexts have an array of string and of
> same length.
> Now in a single loop i want to iterate the values of both the context. But
i
> am not able to do that.
> what i was trying to do is that:-
> #set($counter = 0)
>
> #foreach($ctx1 in $context1)
>
> <select name="combobox">
>
> <option value="$context2[$counter]">$ctx1</option>
>
> </select>
>
> #end
>
> Here in the foreach loop $ctx1 is returning me the exact values but the
> $context2[$counter] is returning me the object of the array string.
>
> so i am not able to get the values. So is there any way in which i can
> iterate the two context in a single foreach loop or any another way you
can
> suggest.
>
> Thanks in Advance
>
> Tarun
>
>
>
>
>
> ---------------------------------------------------------------------
> 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: How to iterate the two different context in a single foreach loop

Posted by avanish <av...@secf.com>.
Why r u using two different contexts ?
Also this problem can be solved it we do context chaining....

----- Original Message -----
From: "Tarun" <ta...@secf.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Wednesday, June 22, 2005 5:35 PM
Subject: How to iterate the two different context in a single foreach loop


hii
I am new to velocity
i am getting two context on the velocity template say
context1 and context2. Both these contexts have an array of string and of
same length.
Now in a single loop i want to iterate the values of both the context. But i
am not able to do that.
what i was trying to do is that:-
#set($counter = 0)

#foreach($ctx1 in $context1)

<select name="combobox">

<option value="$context2[$counter]">$ctx1</option>

</select>

#end

Here in the foreach loop $ctx1 is returning me the exact values but the
$context2[$counter] is returning me the object of the array string.

so i am not able to get the values. So is there any way in which i can
iterate the two context in a single foreach loop or any another way you can
suggest.

Thanks in Advance

Tarun





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