You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by danny van elsen <da...@skynet.be> on 2010/02/13 22:13:34 UTC
classes + methods
hello all,
I'm new to velocity, and I'm trying to get something general to work for
different kinds of inputs + templates.
I'm doing something like:
*******************
class Velo {
public static StringList [] stringlist;
public static void main(String[] args)
{
Velo v = new Velo();
v.run(args);
}
public void run (String[] args)
{
...fill stringlists with data ...
... initialise velocity ...
for (i = 0; i < ... ; i++)
context.put(el_name[i], stringlist[i].sl);
...
}
}
***********************************
public class StringList
{
public String name;
public StringRow [] sl;
public StringList()
{
name = "";
... initialse stringrows ...
}
}
************************************
public class StringRow
{
public String N;
public ArrayList <String> sr;
public StringRow()
{
sr = new ArrayList <String> ();
}
public String getN()
{
return N;
}
}
************************************
but when running the template entry
#foreach( $res in $result )
<td> ${res}.getN() </td>
#end
I'm only getting
<td> StringRow@7e0c2ff5.getN() </td>
= the method getN() isn't being translated in to the underlying String
so my questions are :-)
1. why won't this work?
2. can something like <td> ${res}.sr.get(0) </td> work?
i.e. calling a method (= get(0)) on a field (sr) of the context
element (${res}) that is actually an array?
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: classes + methods
Posted by Nathan Bubna <nb...@gmail.com>.
Use a RenderTool (see VelocityTools) or put the context in the context
and use $context.get('cur_${c}')
On Sun, Feb 21, 2010 at 3:10 PM, danny van elsen
<da...@skynet.be> wrote:
> hello all,
>
> ok, I've managed to write a Java program that will treat input data in a
> generic way.
>
> for example, the data for the EUR currency
> stringlist cur_EUR year value1 value2
> stringlist cur_EUR 2009 EUR-2009-1 EUR-2009-2
> stringlist cur_EUR 2010 EUR-2010-1 EUR-2010-2
>
> (
> the program will do
> context.put("cur_EUR", my_object_that_contains_the_three_data_lines)
> )
>
> will be interpreted by
> #foreach( $res in $cur_EUR )
> #if ($velocityCount > 0)
> <td> $res.get_string(0) : $res.get_string(1) :
> $res.getColumn("year") </td>
> #end
> #end
>
> to output the corresponding data in cur_EUR
>
> so far so good...
>
> but what if I want to carry this to one level higher, and do not know in
> advance how many currencies will have to be treated?
>
> with the input data
> stringrow currencies EUR USD JPY
> stringlist cur_EUR year value1 value2
> stringlist cur_EUR 2009 EUR-2009-1 EUR-2009-2
> stringlist cur_EUR 2010 EUR-2010-1 EUR-2010-2
> stringlist cur_USD year value1 value2
> stringlist cur_USD 2009 USD-2009-1 USD-2009-2
> stringlist cur_USD 2010 USD-2010-1 USD-2010-2
> stringlist cur_USD 2011 USD-2011-1 USD-2011-2
>
> I thought I could do something like
> #foreach( $c in $currencies ) <-- this will yield [EUR, USD,
> JPY]
> #foreach( $y in cur_${c} ) <-- this would have to give
> [cur_EUR, cur_USD, cur_JPY]
> $y.get_string(0)
> #end
> #end
>
> but this will not work?
>
> is there any way that the container in the #foreach statement can be the
> concatenation of a literal and the result of a previous Velocity
> variable interpretation?
>
>
> On Mon, 2010-02-15 at 15:20 -0700, ChadDavis wrote:
>
>> I have had no trouble working with maps and collections of all kinds.
>>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: classes + methods
Posted by danny van elsen <da...@skynet.be>.
hello all,
ok, I've managed to write a Java program that will treat input data in a
generic way.
for example, the data for the EUR currency
stringlist cur_EUR year value1 value2
stringlist cur_EUR 2009 EUR-2009-1 EUR-2009-2
stringlist cur_EUR 2010 EUR-2010-1 EUR-2010-2
(
the program will do
context.put("cur_EUR", my_object_that_contains_the_three_data_lines)
)
will be interpreted by
#foreach( $res in $cur_EUR )
#if ($velocityCount > 0)
<td> $res.get_string(0) : $res.get_string(1) :
$res.getColumn("year") </td>
#end
#end
to output the corresponding data in cur_EUR
so far so good...
but what if I want to carry this to one level higher, and do not know in
advance how many currencies will have to be treated?
with the input data
stringrow currencies EUR USD JPY
stringlist cur_EUR year value1 value2
stringlist cur_EUR 2009 EUR-2009-1 EUR-2009-2
stringlist cur_EUR 2010 EUR-2010-1 EUR-2010-2
stringlist cur_USD year value1 value2
stringlist cur_USD 2009 USD-2009-1 USD-2009-2
stringlist cur_USD 2010 USD-2010-1 USD-2010-2
stringlist cur_USD 2011 USD-2011-1 USD-2011-2
I thought I could do something like
#foreach( $c in $currencies ) <-- this will yield [EUR, USD,
JPY]
#foreach( $y in cur_${c} ) <-- this would have to give
[cur_EUR, cur_USD, cur_JPY]
$y.get_string(0)
#end
#end
but this will not work?
is there any way that the container in the #foreach statement can be the
concatenation of a literal and the result of a previous Velocity
variable interpretation?
On Mon, 2010-02-15 at 15:20 -0700, ChadDavis wrote:
> I have had no trouble working with maps and collections of all kinds.
>
Re: classes + methods
Posted by ChadDavis <ch...@gmail.com>.
I have had no trouble working with maps and collections of all kinds.
On Mon, Feb 15, 2010 at 2:48 PM, danny van elsen
<da...@skynet.be> wrote:
> ok, given
> public class StringRow
> {
> public String N;
>
> public ArrayList <String> sr;
>
> public StringRow(Integer i)
> {
> sr = new ArrayList <String> ();
> N = "heej" + Integer.toString(i) ;
> }
>
> public String getsr(Integer i)
> {
> return sr.get(i);
> }
>
> public String getN()
> {
> return N;
> }
> }
>
> and the template
> #foreach( $res in $result )
> -hallo-
> <td> {$res}.size() </td>
> <td> $res.N </td>
> #end
>
> I now get
> -hallo-
> <td> {velsd.StringRow@7854a328}.size() </td>
> <td> heej01 </td>
> -hallo-
> <td> {velsd.StringRow@7ca3d4cf}.size() </td>
> <td> heej02 </td>
> and so on
>
> is there any way that the $res in
> #foreach( $res in $result )
> can itself refer to a new array?
>
> reading the documentation, I was led to believe that this array could be
> a ArrayList, but perhaps it has to be a fixed size array?
>
> greetings, danny.
>
>
>
> On Sat, 2010-02-13 at 15:34 -0700, ChadDavis wrote:
>
>> Those braces go around everything in the velocity reference.
>>
>> ${res}.getN()
>>
>> should be
>>
>> ${res.getN()}
>>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: classes + methods
Posted by danny van elsen <da...@skynet.be>.
ok, given
public class StringRow
{
public String N;
public ArrayList <String> sr;
public StringRow(Integer i)
{
sr = new ArrayList <String> ();
N = "heej" + Integer.toString(i) ;
}
public String getsr(Integer i)
{
return sr.get(i);
}
public String getN()
{
return N;
}
}
and the template
#foreach( $res in $result )
-hallo-
<td> {$res}.size() </td>
<td> $res.N </td>
#end
I now get
-hallo-
<td> {velsd.StringRow@7854a328}.size() </td>
<td> heej01 </td>
-hallo-
<td> {velsd.StringRow@7ca3d4cf}.size() </td>
<td> heej02 </td>
and so on
is there any way that the $res in
#foreach( $res in $result )
can itself refer to a new array?
reading the documentation, I was led to believe that this array could be
a ArrayList, but perhaps it has to be a fixed size array?
greetings, danny.
On Sat, 2010-02-13 at 15:34 -0700, ChadDavis wrote:
> Those braces go around everything in the velocity reference.
>
> ${res}.getN()
>
> should be
>
> ${res.getN()}
>
Re: classes + methods
Posted by ChadDavis <ch...@gmail.com>.
Those braces go around everything in the velocity reference.
${res}.getN()
should be
${res.getN()}
And you don't even need them. Also, since getN() follows a java beans
getter pattern, you can just say:
$res.n or $res.N
I can never remember what happens with a single letter JavaBeans signature.
On Sat, Feb 13, 2010 at 2:13 PM, danny van elsen
<da...@skynet.be> wrote:
> hello all,
>
> I'm new to velocity, and I'm trying to get something general to work for
> different kinds of inputs + templates.
>
> I'm doing something like:
>
> *******************
> class Velo {
>
> public static StringList [] stringlist;
>
> public static void main(String[] args)
> {
> Velo v = new Velo();
> v.run(args);
> }
>
> public void run (String[] args)
> {
> ...fill stringlists with data ...
>
> ... initialise velocity ...
>
> for (i = 0; i < ... ; i++)
> context.put(el_name[i], stringlist[i].sl);
> ...
> }
> }
>
> ***********************************
>
> public class StringList
> {
> public String name;
>
> public StringRow [] sl;
>
> public StringList()
> {
> name = "";
> ... initialse stringrows ...
> }
> }
>
> ************************************
>
> public class StringRow
> {
> public String N;
>
> public ArrayList <String> sr;
>
> public StringRow()
> {
> sr = new ArrayList <String> ();
> }
>
> public String getN()
> {
> return N;
> }
> }
>
> ************************************
>
> but when running the template entry
>
> #foreach( $res in $result )
> <td> ${res}.getN() </td>
> #end
>
> I'm only getting
> <td> StringRow@7e0c2ff5.getN() </td>
> = the method getN() isn't being translated in to the underlying String
>
> so my questions are :-)
> 1. why won't this work?
>
> 2. can something like <td> ${res}.sr.get(0) </td> work?
> i.e. calling a method (= get(0)) on a field (sr) of the context
> element (${res}) that is actually an array?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org