You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Shankar Banerjee <sb...@casecentral.com> on 2004/10/14 00:54:19 UTC

Walking a map in VTL without knowing the keys apriori

We have a map that has been set into the velocity context. In the .vm template 
we have to walk this map. But we need to get the keys of the map first (the 
keys are not known apriori, so we cannot do  ${myMap.myKey} ). When I do a 
#foreach $key in $myMap , then how do I access the map to get the values ? 
Velocity is returning the map as objects and not interpreting them as String 
(which they are). I am trying to find  example of how to walk a map in VTL by 
first getting the keys and then iterating the map with the keys.

Any ideas examples would be highly appreciated.

Thanks
--Shankar



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


Re: Walking a map in VTL without knowing the keys apriori

Posted by Shankar Banerjee <sb...@casecentral.com>.

Thanks a bunch Shinobu. I find your answers very helpful and valuable.

--Shankar




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


RE: Walking a map in VTL without knowing the keys apriori

Posted by Tim Colson <tc...@cisco.com>.
Great answer, Shinobu.... this one is asked so often, I added your answer to
the FAQ (and I only cursed at moin moin wiki briefly) <grin>

http://wiki.apache.org/jakarta-velocity/VelocityFAQ


Cheers,
Timo
 

> -----Original Message-----
> From: Shinobu Kawai [mailto:shinobu.kawai@gmail.com] 
> Sent: Wednesday, October 13, 2004 6:06 PM
> To: Velocity Users List
> Subject: Re: Walking a map in VTL without knowing the keys apriori
> 
> 
> Hi Shankar,
> 
> On Wed, 13 Oct 2004 22:54:19 +0000 (UTC)
> Shankar Banerjee <sb...@casecentral.com> wrote:
> 
> > We have a map that has been set into the velocity context. 
> In the .vm template 
> > we have to walk this map. But we need to get the keys of 
> the map first (the 
> > keys are not known apriori, so we cannot do  ${myMap.myKey} 
> ). When I do a 
> > #foreach $key in $myMap , then how do I access the map to 
> get the values ? 
> > Velocity is returning the map as objects and not 
> interpreting them as String 
> > (which they are). I am trying to find  example of how to 
> walk a map in VTL by 
> > first getting the keys and then iterating the map with the keys.
> #foreach will iterate the values for Map's.  In order to use the keys,
> you will have to do something like:
> 
> #foreach($key in $myMap.keySet())
>  #set($value = $myMap.get($key))
>  $key = $value
> #end
> 
> or you can do
> 
> #foreach($entry in $myMap.entrySet())
>  $entry.key = $entry.value
> #end
> 
> Best regards,
> -- Shinobu Kawai
> 
> --
> Shinobu Kawai <sh...@gmail.com>
> 
> 
> ---------------------------------------------------------------------
> 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: Walking a map in VTL without knowing the keys apriori

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Shankar,

On Wed, 13 Oct 2004 22:54:19 +0000 (UTC)
Shankar Banerjee <sb...@casecentral.com> wrote:

> We have a map that has been set into the velocity context. In the .vm template 
> we have to walk this map. But we need to get the keys of the map first (the 
> keys are not known apriori, so we cannot do  ${myMap.myKey} ). When I do a 
> #foreach $key in $myMap , then how do I access the map to get the values ? 
> Velocity is returning the map as objects and not interpreting them as String 
> (which they are). I am trying to find  example of how to walk a map in VTL by 
> first getting the keys and then iterating the map with the keys.
#foreach will iterate the values for Map's.  In order to use the keys,
you will have to do something like:

#foreach($key in $myMap.keySet())
 #set($value = $myMap.get($key))
 $key = $value
#end

or you can do

#foreach($entry in $myMap.entrySet())
 $entry.key = $entry.value
#end

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <sh...@gmail.com>


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