velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bill Burton <bi...@progress.com>
Subject Re: Map support in VTL
Date Wed, 09 Oct 2002 23:40:37 GMT
Hello,

Paul Smith wrote:
> You know, you could write a Java factory object that parses the text and
> returns a Map, and just
> drop that Java object into the context?
> 
> #set( $myMap = $MapFactory.build("{ "one"=1, "two"=2, "three"=3 }"))

A slight variation to the above is to use the array syntax like this:
     #set( $myMap = $MapFactory.build(["one=1", "two=2", "three=3"]) )

The build method accepts an ArrayList and then iterates over it 
splitting each element on the first "=" putting the key and associated 
value into a HashMap.  Ought to be rather easy to implement if you 
assume all values are Strings.  No complicated parsing required.

> I know that's not quite as good as built in support, but it could easily be
> done.
> 
> I don't think I've seen any language (scripting or otherwise) that supports
> that Map construct, but I am probably going to have to eat my hat as soon as
> I say that.

Attributes in XML/HTML tags are the same idea even though the Map isn't 
accessable.

> I think the reason behind the lack is that the syntax can be a little too
> hard to read for large Maps, and so they never bothered.

Or, because WebMacro on which Velocity is based, didn't have such a 
construct either.

> cheers,
> 
> Paul Smith

-Bill

> -----Original Message-----
> From: Aapo Laakkonen [mailto:aapo.laakkonen@giro.fi]
> Sent: Thursday, 10 October 2002 8:41 AM
> To: 'Velocity Users List'
> Subject: Map support in VTL
> 
> 
> Is it possibly to add map (java.util.Map) support to VTL. What I mean is
> something similar as the current Collection (java.util.ArrayList)
> support:
> 
> #set($list = [ "one", "two", "three" ])
> 
> What I would like to see is Map (java.util.HashMap) support like the
> following:
> 
> #set($map = { "one"=1, "two"=2, "three"=3 })
> 
> Is this too much asked? I'm not sure about the syntax above, but
> something similar. Currently I have to do something like this to
> archieve the same results:
> 
> #set($map = $Global.map)
> #set($map.one = 1)
> #set($map.two = 2)
> #set($map.three = 3)
> 
> It's fine, but not as compact as I would like it to be. Look for example
> the following:
> 
> #set($list = [ { "firstname"="Aapo", "lastname"="laakkonen" }, "text" ]
> 
> Seems simple, but this is what I have to do right now:
> 
> #set($list = []) ## or #set($list = $Global.list) 
> 
> $list.add(0, $Global.map)
> #set($map = $list.get(0))
> 
> #set($map.firstname = "Aapo")
> #set($map.lastname = "Laakkonen")
> 
> $list.add(1, "text")
> 
> As you see in this simple example, this second approach requires much
> more typing.
> 
> Kind Regards
> Aapo Laakkonen


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


Mime
View raw message