You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Cort Schaefer <co...@xmission.com> on 2002/09/06 17:01:11 UTC
RE: Is there a way to tell when iterating in #foreach if you have
com e to the last element.
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:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
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 Barbara Baughman <ba...@utdallas.edu>.
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:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: Getting no space after an ending directive
Posted by Barbara Baughman <ba...@utdallas.edu>.
The null comment after #else/#end does work!
Thanks.
Barbara Baughman
X2157
On Fri, 6 Sep 2002, Charles N. Harvey III wrote:
> I don't think any language will let you do that really. You are changing
> the directive word to #elsenull instead of #else.
>
> Just off the top of my head though:
>
> department=#if ($dept)$dept#else#**#null#end
>
>
> Throw a comment in between there to break it up. Should give you any space.
> But I may be wrong.
>
>
> Charlie
>
> -----Original Message-----
> From: Barbara Baughman [mailto:baughman@utdallas.edu]
> Sent: Friday, September 06, 2002 12:57 PM
> To: Velocity Users List
> Subject: Getting no space after an ending directive
>
>
> I was trying to output something after an #if statement that would not
> have a space, but it looks like Velocity will not recognize an #end or
> #else unless there is a space after it.
>
> department=#if ($dept)$dept#elsenull#end
>
> ctx.put("dept","something);
>
> department=something#elsenull
>
> I would like
> department=something
>
>
> If the value of $dept was not set, I would like the output
> department=null
>
> Instead the output is
> department=
>
>
> This happens with velocity-1.3.1-rc2 and velocity-1.3-rc1, and I have
> no specially defined directive called #elsenull. Is there any special way
> to delimit the end of #end or #else that makes this work the way I wish?
>
> Thanks
>
> 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:
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> >
> > --
> > To unsubscribe, e-mail:
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> >
> > --
> > To unsubscribe, e-mail:
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> >
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
>
> --
> To unsubscribe, e-mail: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: Getting no space after an ending directive
Posted by "Charles N. Harvey III" <ch...@alloy.com>.
I don't think any language will let you do that really. You are changing
the directive word to #elsenull instead of #else.
Just off the top of my head though:
department=#if ($dept)$dept#else#**#null#end
Throw a comment in between there to break it up. Should give you any space.
But I may be wrong.
Charlie
-----Original Message-----
From: Barbara Baughman [mailto:baughman@utdallas.edu]
Sent: Friday, September 06, 2002 12:57 PM
To: Velocity Users List
Subject: Getting no space after an ending directive
I was trying to output something after an #if statement that would not
have a space, but it looks like Velocity will not recognize an #end or
#else unless there is a space after it.
department=#if ($dept)$dept#elsenull#end
ctx.put("dept","something);
department=something#elsenull
I would like
department=something
If the value of $dept was not set, I would like the output
department=null
Instead the output is
department=
This happens with velocity-1.3.1-rc2 and velocity-1.3-rc1, and I have
no specially defined directive called #elsenull. Is there any special way
to delimit the end of #end or #else that makes this work the way I wish?
Thanks
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:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: Getting no space after an ending directive
Posted by cort <co...@xmission.com>.
Try:
#set($null = null)
department=#if ($dept)$dept#else$null#end
-----Original Message-----
From: Barbara Baughman [mailto:baughman@utdallas.edu]
Sent: Friday, September 06, 2002 10:57 AM
To: Velocity Users List
Subject: Getting no space after an ending directive
I was trying to output something after an #if statement that would not
have a space, but it looks like Velocity will not recognize an #end or
#else unless there is a space after it.
department=#if ($dept)$dept#elsenull#end
ctx.put("dept","something);
department=something#elsenull
I would like
department=something
If the value of $dept was not set, I would like the output
department=null
Instead the output is
department=
This happens with velocity-1.3.1-rc2 and velocity-1.3-rc1, and I have
no specially defined directive called #elsenull. Is there any special
way
to delimit the end of #end or #else that makes this work the way I wish?
Thanks
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:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Getting no space after an ending directive
Posted by Barbara Baughman <ba...@utdallas.edu>.
I was trying to output something after an #if statement that would not
have a space, but it looks like Velocity will not recognize an #end or
#else unless there is a space after it.
department=#if ($dept)$dept#elsenull#end
ctx.put("dept","something);
department=something#elsenull
I would like
department=something
If the value of $dept was not set, I would like the output
department=null
Instead the output is
department=
This happens with velocity-1.3.1-rc2 and velocity-1.3-rc1, and I have
no specially defined directive called #elsenull. Is there any special way
to delimit the end of #end or #else that makes this work the way I wish?
Thanks
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:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>