velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Barbara Baughman <baugh...@utdallas.edu>
Subject RE: Is there a way to tell when iterating in #foreach if you have com e to the last element.
Date Fri, 06 Sep 2002 15:58:07 GMT
Very cool!  I had added an isLast() method to my array object, but this
takes care of it neatly.

Barbara Baughman
X2157

On Fri, 6 Sep 2002, 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
> 
> 
> 
> -----Original Message-----
> From: Jason Weinstein [mailto:JWeinstein@SeeBeyond.com] 
> Sent: Thursday, September 05, 2002 8:50 PM
> To: 'Velocity Users List'
> Subject: Is there a way to tell when iterating in #foreach if you have com e
> to the last element.
> 
> Is there a way to tell when iterating in #foreach if you have come to the
> last element.
> 
> Maybe the velocity team can plug in a  
> $hasNext = true|false
> as it already maintains a loop $velocityCount variable.
> 
> Use (Java code):
> 
> System.out.println(evaluate("#foreach ($obj in
> ${this.breadthFirstEnumeration()})${obj.userObject}.#end", root)));
> 
> Note in this case root is javax.swing.tree.DefaultMutableTreeNode
> 
> Want to plug in #if($hasNext).#end
> 
> I want to be able to delimit the list with a '.'
> 
> Printing something like "root.a.b.c" instead of "root.a.b.c."
> 
> Code (Java code):
> 
> Generic toString method (applys Object to template)
> 
> Thinking useful for testcases where you assert things based on internal
> values. Such as construct tree and assert evaluate(...).equals("root.a.b.c")
> 
> public static String evaluate(String template, Object obj) {
>     String retval = null;
>     try {
>         Velocity.init();
>         VelocityContext context = new VelocityContext();
>         context.put("this", obj);
>         StringWriter out = new StringWriter();
>         boolean bRet = Velocity.evaluate(context, out, "test", template);
>         if (bRet) {
>             retval = out.toString();
>         }
>         else {
>             retval = (obj == null) ? null: obj.toString();
>         }
>     }
>     catch (Exception e) {
>         e.printStackTrace();
>     }
>     return retval;
> }
> 
> This seems like an easy feature to add can someone forward this request to
> the right place. Thanks
> 
> --
> To unsubscribe, e-mail:
> <mailto:velocity-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:velocity-user-help@jakarta.apache.org>
> 
> --
> To unsubscribe, e-mail:
> <mailto:velocity-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:velocity-user-help@jakarta.apache.org>
> 
> --
> To unsubscribe, e-mail:
> <mailto:velocity-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:velocity-user-help@jakarta.apache.org>
> 


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


Mime
View raw message