You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by bluejoe <bl...@sdb.cnic.cn> on 2008/12/09 07:12:38 UTC
How Can I Evaluate an Expression in Velocity?
Hi All!
In my project, I want to use Velocity to evaluate user-defined expressions like:
$a + $b
How can I do that? It seems that I cannot use such an expression in a template directly. Then I tried to use #set directive:
#set ($temp = $a + $b)
$temp
It works well, but I doubt if there is a method provided in Velocity such as:
Object o = Velocity.eval(vc, "$a + $b");
Could you give me an answer on this point? I need your help. Thanks a lot!
Best Regards,
bluejoe
Re: How Can I Evaluate an Expression in Velocity?
Posted by bluejoe <bl...@gmail.com>.
Thank you, Walid.
I was using the method you recommended in the past, but I don't think it pretty :)
I will follow Nathan's suggestion to use RenderTool.eval(Context ctx, String vtl)
maybe it is what I am looking for:)
Many thanks!
----- Original Message -----
From: "Walid "jo" Gedeon" <wg...@gmail.com>
To: "Velocity Users List" <us...@velocity.apache.org>
Sent: Tuesday, December 09, 2008 5:28 PM
Subject: Re: How Can I Evaluate an Expression in Velocity?
> Hello bluejoe, Try this:
>
> public static void main(String args[]) throws Exception {
> VelocityEngine engine = new VelocityEngine();
> engine.init();
>
> VelocityContext ctx = new VelocityContext();
> ctx.put("a", 1);
> ctx.put("b", 2);
>
> StringWriter writer = new StringWriter();
> engine.evaluate(ctx, writer, "ERROR", "#set($c = $a + $b)");
> Object c = ctx.get("c");
>
> System.out.println("{"+c.getClass().getName()+"="+c+"}");
> }
>
> HTH,
> w
>
> On Tue, Dec 9, 2008 at 7:12 AM, bluejoe <bl...@sdb.cnic.cn> wrote:
>
>> Hi All!
>>
>> In my project, I want to use Velocity to evaluate user-defined expressions
>> like:
>>
>> $a + $b
>>
>> How can I do that? It seems that I cannot use such an expression in a
>> template directly. Then I tried to use #set directive:
>>
>> #set ($temp = $a + $b)
>> $temp
>>
>> It works well, but I doubt if there is a method provided in Velocity such
>> as:
>>
>> Object o = Velocity.eval(vc, "$a + $b");
>>
>> Could you give me an answer on this point? I need your help. Thanks a lot!
>>
>> Best Regards,
>> bluejoe
>
Re: How Can I Evaluate an Expression in Velocity?
Posted by "Walid \"jo\" Gedeon" <wg...@gmail.com>.
Hello bluejoe, Try this:
public static void main(String args[]) throws Exception {
VelocityEngine engine = new VelocityEngine();
engine.init();
VelocityContext ctx = new VelocityContext();
ctx.put("a", 1);
ctx.put("b", 2);
StringWriter writer = new StringWriter();
engine.evaluate(ctx, writer, "ERROR", "#set($c = $a + $b)");
Object c = ctx.get("c");
System.out.println("{"+c.getClass().getName()+"="+c+"}");
}
HTH,
w
On Tue, Dec 9, 2008 at 7:12 AM, bluejoe <bl...@sdb.cnic.cn> wrote:
> Hi All!
>
> In my project, I want to use Velocity to evaluate user-defined expressions
> like:
>
> $a + $b
>
> How can I do that? It seems that I cannot use such an expression in a
> template directly. Then I tried to use #set directive:
>
> #set ($temp = $a + $b)
> $temp
>
> It works well, but I doubt if there is a method provided in Velocity such
> as:
>
> Object o = Velocity.eval(vc, "$a + $b");
>
> Could you give me an answer on this point? I need your help. Thanks a lot!
>
> Best Regards,
> bluejoe
Re: How Can I Evaluate an Expression in Velocity?
Posted by Nathan Bubna <nb...@gmail.com>.
That's pretty much just for within VTL; he was asking about an
external, Java API. :)
On Tue, Dec 9, 2008 at 7:55 AM, Will Glass-Husain
<wg...@gmail.com> wrote:
> What about #evaluate? (introduced in v 1.6)
>
> Will
>
> On Dec 9, 2008, at 6:56 AM, "Nathan Bubna" <nb...@gmail.com> wrote:
>
>> There is a RenderTool class in the VelocityTools project which can do
>> most of this for you.
>>
>> On Mon, Dec 8, 2008 at 10:12 PM, bluejoe <bl...@sdb.cnic.cn> wrote:
>>>
>>> Hi All!
>>>
>>> In my project, I want to use Velocity to evaluate user-defined
>>> expressions like:
>>>
>>> $a + $b
>>>
>>> How can I do that? It seems that I cannot use such an expression in a
>>> template directly. Then I tried to use #set directive:
>>>
>>> #set ($temp = $a + $b)
>>> $temp
>>>
>>> It works well, but I doubt if there is a method provided in Velocity such
>>> as:
>>>
>>> Object o = Velocity.eval(vc, "$a + $b");
>>>
>>> Could you give me an answer on this point? I need your help. Thanks a
>>> lot!
>>>
>>> Best Regards,
>>> bluejoe
>>
>> ---------------------------------------------------------------------
>> 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
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: How Can I Evaluate an Expression in Velocity?
Posted by Will Glass-Husain <wg...@gmail.com>.
What about #evaluate? (introduced in v 1.6)
Will
On Dec 9, 2008, at 6:56 AM, "Nathan Bubna" <nb...@gmail.com> wrote:
> There is a RenderTool class in the VelocityTools project which can do
> most of this for you.
>
> On Mon, Dec 8, 2008 at 10:12 PM, bluejoe <bl...@sdb.cnic.cn> wrote:
>> Hi All!
>>
>> In my project, I want to use Velocity to evaluate user-defined
>> expressions like:
>>
>> $a + $b
>>
>> How can I do that? It seems that I cannot use such an expression in
>> a template directly. Then I tried to use #set directive:
>>
>> #set ($temp = $a + $b)
>> $temp
>>
>> It works well, but I doubt if there is a method provided in
>> Velocity such as:
>>
>> Object o = Velocity.eval(vc, "$a + $b");
>>
>> Could you give me an answer on this point? I need your help. Thanks
>> a lot!
>>
>> Best Regards,
>> bluejoe
>
> ---------------------------------------------------------------------
> 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
Re: How Can I Evaluate an Expression in Velocity?
Posted by Nathan Bubna <nb...@gmail.com>.
There is a RenderTool class in the VelocityTools project which can do
most of this for you.
On Mon, Dec 8, 2008 at 10:12 PM, bluejoe <bl...@sdb.cnic.cn> wrote:
> Hi All!
>
> In my project, I want to use Velocity to evaluate user-defined expressions like:
>
> $a + $b
>
> How can I do that? It seems that I cannot use such an expression in a template directly. Then I tried to use #set directive:
>
> #set ($temp = $a + $b)
> $temp
>
> It works well, but I doubt if there is a method provided in Velocity such as:
>
> Object o = Velocity.eval(vc, "$a + $b");
>
> Could you give me an answer on this point? I need your help. Thanks a lot!
>
> Best Regards,
> bluejoe
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org