You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Jo...@reasoning.com on 2002/09/06 18:12:54 UTC
RE: Is there a way to tell when iterating in #foreach if you have com e to the last element.
> 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.
Joshua Levy
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Template Tips and Tricks user-guide patch & bug?
Posted by Tim Colson <tc...@cisco.com>.
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 = ', ' )
#end
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. :-)
Cheers,
Tim
-----Original Message-----
From: Christoph.Reck@dlr.de [mailto:Christoph.Reck@dlr.de]
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
$list)#if($velocityCount>1).#end$o#end
>
>
Geir Magnusson Jr. wrote:
> On 9/6/02 12:12 PM, "Joshua.Levy@reasoning.com"
> <Jo...@reasoning.com>
> 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:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
Re: Is there a way to tell when iterating in #foreach if you have
com e to the last element.
Posted by Ch...@dlr.de.
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 $list)#if($velocityCount>1).#end$o#end
>
>
Geir Magnusson Jr. wrote:
> On 9/6/02 12:12 PM, "Joshua.Levy@reasoning.com" <Jo...@reasoning.com>
> 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: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Is there a way to tell when iterating in #foreach if you have com
e to the last element.
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 9/6/02 12:12 PM, "Joshua.Levy@reasoning.com" <Jo...@reasoning.com>
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.
--
Geir Magnusson Jr.
Research & Development, Adeptra Inc.
geirm@adeptra.com
+1-203-247-1713
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>