velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "bluejoe" <bluejoe2...@gmail.com>
Subject Re: How Can I Evaluate an Expression in Velocity?
Date Wed, 10 Dec 2008 00:56:34 GMT
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" <wgedeon@gmail.com>
To: "Velocity Users List" <user@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 <bluejoe@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
>
Mime
View raw message