velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Kienenberger <mkien...@alaska.net>
Subject Re: Chop output...
Date Fri, 14 Jan 2005 16:25:33 GMT
Dimitrios Kolovos <dskolovos@gmail.com> wrote:
> When iterating a collection/array it is usual that you add something after 

> each iteration except for the last.
> 
> #foreach ($i in [1..10])$i,#end
> 
> produces
> 
> 1,2,3..10,
> 
> The only way I know in which I can omit the last comma is to use 
> velocityount. Nevertheless, I think it would be more elegant if there was 
a 
> variable (e.g. $output) in the context that represented the so far 
produced 
> text.

Most of us just use an #if statement and only output the comma on non-first 
elements.  Here's one way to do it off the top of my head -- could be syntax 
errors.  Some of the ##s are probably overkill, but better safe than sorry.  
Or you could cram it all on one line.

#set( $tempFirstOne = "true" )##
#foreach ($i in [1..10])##
#if ($tempFirstOne = "true")##
#set( $tempFirstOne = "false" )##
#else## not $tempFirstOne
,##
#end###if ($tempFirstOne = "true")
$i##
#end##foreach ($i in [1..10])

-Mike

And as long as we're talking about #foreach#else#end, how about

#onFirst?
#onLast?
#onEmpty?

  :-)

Not unreasonable in a templating language, and definitely more readable than 
the above.

-Mike

---------------------------------------------------------------------
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