velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tim Colson" <>
Subject Template Tips and Tricks user-guide patch & bug?
Date Tue, 10 Sep 2002 16:16:50 GMT
Say folks -
  I think this "join list items" template usage is a perfect example of
cool template snippet that would make a fine addition for the Users
Guide under a heading of "Template Tips and Tricks"... or something. :-)

  I decided to [finally] put my doc where my mouth is...and started to
add this to the user-guide.xml to submit as a patch! 

  Before submitting the example code, I figured I'd try it out - and was
surprised by whitespace problems. The code, used inline as Christoph
wrote it, doesn't work on my machine. :-(

#set( $list = ["Lions", "Tigers", "Bears"] )
#set( $sep = '' )
#foreach($o in $list)$sep$o#set( $sep = ', ' )#end

Yields this incorrect output:
Lions#set( = ', ' ) Tigers#set( = ', ' ) Bears#set( = ', ' ) 

If I add in a ## and move the #set to a new line, it works as expected

#set( $list = ["Lions", "Tigers", "Bears"] )
#set( $sep = '' )        
#foreach($o in $list)$sep$o## chomp unwanted whitespace
#set( $sep = ', ' )

Using Velocity 1.3 (I haven't tried with 1.3.1RC2) and an extended
VelocityViewServlet from the Struts tools project.

Anyway - I attached a diff that has the version that does work. 

Geir, please do with it as you see fit. :-)


-----Original Message-----
From: [] 
Sent: Monday, September 09, 2002 5:23 AM
To: Velocity Users List
Subject: Re: Is there a way to tell when iterating in #foreach if you
have com e to the last element.

To avoid depending on how the $velocityCount is configured, I prefer to
use the implicit form: 

#set( $sep = '' ) 
#foreach($o in $list)$sep$o#set( $sep = '.' )#end

:) Christoph Reck

Cort Schaefer wrote:
 > In the case that you proposed, you could also base it on whether you
are on  > the first element or not, ie:  >  > #foreach($o in

Geir Magnusson Jr. wrote:
> On 9/6/02 12:12 PM, "" 
> <>
> wrote:
>>>Maybe the velocity team can plug in a
>>>$hasNext = true|false
>>>as it already maintains a loop $velocityCount variable.
>>If you do this, then I vote for $velocityHasNext or something else 
>>which minimizes the namespace confusion.  With velocity, you are 
>>already dealing with the Java namespace, and the velocity 
>>macro/variable namespace.  It would be nice if all the velocity 
>>created variables had related names.
> The string 'velocityCount' is actually configurable.  I'm not sure 
> that in itself was a good thing, but it does give you the freedom to 
> change it.  The downside, of course, is that if you do change it for 
> your template, it will be unreadable for people who don't know you are

> changing the loop var name behind the scenes.

:) Christoph Reck

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message