velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jason Weinstein <JWeinst...@SeeBeyond.com>
Subject Is there a way to tell when iterating in #foreach if you have com e to the last element.
Date Fri, 06 Sep 2002 02:50:24 GMT
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>


Mime
View raw message