You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Marvin Greenberg <mg...@dctd.saic.com> on 2003/02/10 22:43:17 UTC
Useful addition to #foreach
#foreach already has $velocityCount. Add $isFirst and $isLast. Simple to
write a macro for this...
Just add (somewhere)
#set ($isLast = $velocityCount.equals( $arg3.size() ) )
#set ($isFirst = $velocityCount.equals( 1 ) )
#foreach ($obj in $List)
#if ($isFirst) <table> #end
<tr><td>$obj.Attribute</td></tr>
#if ($isLast) </table> #end
#end
to the foreach definition.
Note that there is a problem with global variables like these (including
velocityCount) when loops are nested, and there is no obvious solution.
Maybe allow a kludgy index by the loop variable name...
$velocityCount.of("obj") or $isFirst.for("obj")...
Better would be $obj.count(), $obj.isFirst() but that would require
dynamically generating a specialization of whatever class $obj happens to be
at each assignment (since you want $obj to behave like the kind of object
the designer expects). Hardly worth the complexity.
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Useful addition to #foreach
Posted by Daniel Dekany <dd...@freemail.hu>.
Monday, February 10, 2003, 10:43:17 PM, Marvin Greenberg wrote:
> #foreach already has $velocityCount. Add $isFirst and $isLast. Simple to
> write a macro for this...
> Just add (somewhere)
> #set ($isLast = $velocityCount.equals( $arg3.size() ) )
> #set ($isFirst = $velocityCount.equals( 1 ) )
>
> #foreach ($obj in $List)
> #if ($isFirst) <table> #end
> <tr><td>$obj.Attribute</td></tr>
> #if ($isLast) </table> #end
> #end
In the concrete situation above maybe it is better practice to do
something like this (sorry if my Vel. syntax is bad... I didn't used
Vel. for a year or so...):
#if (!$obj.isEmpty())
<table>
#foreach ($obj in $List)
<tr><td>$obj.Attribute</td></tr>
#end
</table>
#end
but in the case of Vel. it has the big problem that the template will
not work if $obj is not a Collection Java object. The same problem with
$arg3.size() in isLast macro...
But anyway, something like $velocityIsLast in a template language is
definitely useful. It often happens that you want to know if you are in
the last loop or not. Say, if you want to put coma after all items,
except after the last item.
--
Best regards,
Daniel Dekany
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Useful addition to #foreach
Posted by Bill Burton <bi...@progress.com>.
Hello,
Marvin Greenberg wrote:
> #foreach already has $velocityCount. Add $isFirst and $isLast. Simple to
> write a macro for this...
> Just add (somewhere)
> #set ($isLast = $velocityCount.equals( $arg3.size() ) )
> #set ($isFirst = $velocityCount.equals( 1 ) )
>
> #foreach ($obj in $List)
> #if ($isFirst) <table> #end
> <tr><td>$obj.Attribute</td></tr>
> #if ($isLast) </table> #end
> #end
>
> to the foreach definition.
>
> Note that there is a problem with global variables like these (including
> velocityCount) when loops are nested, and there is no obvious solution.
> Maybe allow a kludgy index by the loop variable name...
You can always just save the value in each outer loop:
#foreach ($obj in $List)
#set($outerCount = $velocityCount)
#foreach ($item in $obj)
...
>
> $velocityCount.of("obj") or $isFirst.for("obj")...
>
> Better would be $obj.count(), $obj.isFirst() but that would require
> dynamically generating a specialization of whatever class $obj happens to be
> at each assignment (since you want $obj to behave like the kind of object
> the designer expects). Hardly worth the complexity.
The best answer is an iterator wrapper tool. This avoids global variables
and can provide ways to start or end the iteration, get the index,
determine if last iteration in the loop, etc., etc. It happens someone
has already written such a tool called FisherMill. It was supposed to be
comitted to the tools subproject but somehow that never happened. You
can grab it at
http://marc.theaimsgroup.com/?l=velocity-user&m=103176242630349&w=2.
-Bill
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org