velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christoph Reck <apa...@recks.org>
Subject Re: A Dynamic ArrayList Literal?
Date Thu, 15 Jul 2004 07:43:49 GMT
Hi,

Chris.Mollo@MSCsoftware.com wrote:
> I would like to store an ArrayList literal in a Velocity variable and then
> use the stored ArrayList literal to establish a new ArrayList (and its 
> contents),
> e.g. I'd like to do something like:
> 
> #set($names = '["Bill","Harry","Chris","Sue"]')
                 ^                              ^
Your problem is that $names is a string; as Nathan already pointed
out - the only way to convert it into an array is via a tool.

If you are nunning under Java 1.4, the String class has a lot of
neat new features built-in. Specifically the split(...) method.

BTW. I've sucessfully created a CSV-Parser in pure velocity
as a macro! Something similar could be used here (avoiding
a tool). If anyone is interested, I can post it to the Wiki.
I have a command line TemplateTool based on velocity to do
file transformations like XML and CSV using templates to format
the output.

> 
> #set($namesArrayList = $names)
> #foreach($name in $namesArrayList)
>    $name
> #end
> 
> However, the above VTL results in $namesArrayList
> being a variable (not an ArrayList) and hence the
> foreach directive effectively does nothing.
> 
> Is there a way to do what I am attempting here?
> In the second #set directive, I want $names to be
> evaluated (substitution occurs) before the assignment
> takes place.
> 
> Thanks!!
> 
> Chris
> 

Whoa! today is the do-away-with-superflous-quotes-day (tm) ;)


In the past I've used velocity to setup an empty array
ant then filled it up with values during rendering. E.g.:
#set( $errors = [] )
...
#call( $errors.add("something weird happened") )
...
## the layout template then uses it:
#if( !errors.isEmpty() )
   #formatErrors( $errors )
#end


Cheers,
Christoph

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


Mime
View raw message