You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Charles Anthony <ch...@hpdsoftware.com> on 2001/06/29 11:48:28 UTC

ArrayList in VM - am I doing something wrong ?

Dear all,

#include("StandardNewbieDisclamer.vm")
#include("VelocityIsReallyGood.vm")

Oh, and I aplogise for the travesty that is the appended disclaimer at the
end of this mail. It is automatically appended at our mail server by order
of the somewhat paranoid management. I have complained, but to no avail.

Righto, I am using Velocity 1.1

Using the standard VM tablerow example as an illustration of my confusion :

#macro( tablerow  $array $color )
#foreach( $element in $array )
   <tr><td bgcolor=$color>$element</td></tr>
#end
#end

If I call it with an array list passed directly (as oppsed to a reference),
with just one item, all  is well :

<table>#tablerow(["A"] "yellow")<table>

If I call it with 

#set($foo = "A")
<table>#tablerow( [ $foo ] "yellow")<table>

I get the following exception : 
org.apache.velocity.exception.ParseErrorException: Encountered "]" at line
21, column 25. Was expecting: ".." ... 

Is this expected behaviour ? Well, I didn't expect it, but you know what I
mean.

The following does work :

#set($foo = "A")
#set($bar = [$foo])
<table>#tablerow( $foo  "yellow")</table>

But I don't really want to use that, if at all possible. Basically, I'm
using a macro to build an (commonly formatted) HTML table and I want the
designers to be able to do :

<table>
#tableRow("RowLabel1", [ $Column1, $Column2 ] )
#tableRow("RowLabel2", [ $Column1, $Column2 ] )
</table>

However, I need to be able to support 1..n columns.  e.g.
<table>
#tableRow("RowLabel1", [ $Column1 ] )
#tableRow("RowLabel2", [ $Column1 ] )
</table>

I could do it like this :

<table>
#set($cols = [$Column1])
#tableRow("RowLabel1",  $cols )
#set($cols = [$Column2])
#tableRow("RowLabel2", $cols )
</table>

But, to my mind, that's fairly verbose piece of naffness.

Am I missing something terribly obvious here ?

With many thanks,

Charles Anthony.



This email and any attachments are strictly confidential and are intended
solely for the addressee. If you are not the intended recipient you must
not disclose, forward, copy or take any action in reliance on this message
or its attachments. If you have received this email in error please notify
the sender as soon as possible and delete it from your computer systems.
Any views or opinions presented are solely those of the author and do not
necessarily reflect those of HPD Software Limited or its affiliates.

 At present the integrity of email across the internet cannot be guaranteed
and messages sent via this medium are potentially at risk.  All liability
is excluded to the extent permitted by law for any claims arising as a re-
sult of the use of this medium to transmit information by or to 
HPD Software Limited or its affiliates.



Re: ArrayList in VM - am I doing something wrong ?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
"Geir Magnusson Jr." wrote:
> 
> Charles Anthony wrote:
> >
> > Dear all,
> >
> > #include("StandardNewbieDisclamer.vm")
> > #include("VelocityIsReallyGood.vm")
> 
> You may want to #parse() these :)
> 
> > Righto, I am using Velocity 1.1
> >
> > Using the standard VM tablerow example as an illustration of my confusion :
> >
> > #macro( tablerow  $array $color )
> > #foreach( $element in $array )
> >    <tr><td bgcolor=$color>$element</td></tr>
> > #end
> > #end
> >
> > If I call it with an array list passed directly (as oppsed to a reference),
> > with just one item, all  is well :
> >
> > <table>#tablerow(["A"] "yellow")<table>
> >
> > If I call it with
> >
> > #set($foo = "A")
> > <table>#tablerow( [ $foo ] "yellow")<table>
> >
> > I get the following exception :
> > org.apache.velocity.exception.ParseErrorException: Encountered "]" at line
> > 21, column 25. Was expecting: ".." ...
> >
> > Is this expected behaviour ? Well, I didn't expect it, but you know what I
> > mean.
> 
> No, it's a parser bug.  I verified it.  I will fix.  Ug.  We haven't
> seen a parser bug for a while :)

Ok - this should be fixed now.  I checked the changes and enhancements
to the testbed in yesterday evening. This will be in the CVS HEAD right
now. The change seems pretty harmless, so I might backport it into the
1.1.1 release, which I will start whenever it rains.  ( My wife sensibly
doesn't like me sitting here on beautiful days ...)

geir


geir
-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Re: ArrayList in VM - am I doing something wrong ?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Charles Anthony wrote:
> 
> Dear all,
> 
> #include("StandardNewbieDisclamer.vm")
> #include("VelocityIsReallyGood.vm")

You may want to #parse() these :)

> Righto, I am using Velocity 1.1
> 
> Using the standard VM tablerow example as an illustration of my confusion :
> 
> #macro( tablerow  $array $color )
> #foreach( $element in $array )
>    <tr><td bgcolor=$color>$element</td></tr>
> #end
> #end
> 
> If I call it with an array list passed directly (as oppsed to a reference),
> with just one item, all  is well :
> 
> <table>#tablerow(["A"] "yellow")<table>
> 
> If I call it with
> 
> #set($foo = "A")
> <table>#tablerow( [ $foo ] "yellow")<table>
> 
> I get the following exception :
> org.apache.velocity.exception.ParseErrorException: Encountered "]" at line
> 21, column 25. Was expecting: ".." ...
> 
> Is this expected behaviour ? Well, I didn't expect it, but you know what I
> mean.

No, it's a parser bug.  I verified it.  I will fix.  Ug.  We haven't
seen a parser bug for a while :)
 
> The following does work :
> 
> #set($foo = "A")
> #set($bar = [$foo])
> <table>#tablerow( $foo  "yellow")</table>

You could also work around for now with

#set($foo = "A")
<table>#tablerow( [ $foo] "yellow")<table>

(no space after $foo)

[SNIP]
> 
> Am I missing something terribly obvious here ?

Nope.  You are right on.  So, just stop with the trailing space before
the ']' so you can keep going, and I will try to knock this out today.
 
geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!