Thank you very much Tony and Shane, this is exactly what I needed.
I guess I'm going to use the RenderTool as it is already available.
Just a question about the example
$render.eval($ctx, "${object}.$method")

Where does $ctx come from? I guess I can get it from $data, as shown in Tony's code, but is there a more simple way to get it?

Again, thank you very much!

BoD


Tony Oslund a écrit :
One more thought...

I am using this to nest a template within a template, within a template....

If you have a nested eval call embedded within the template that is called it 
will be evaluated also...

Try this if you are curious.

#set ($myExpr =  '$row.getColumn("pl_product_line_description")')
$myExpr
#set ($myExprB = '$vsesstool.evalExpression($data,$myExpr)')
$myExprB
$vsesstool.evalExpression($data,$myExprB)

You get the idea.  It is an easy way of handling nested content.

Especially if you are pulling your template content from a different 
source/server, etc... (rather than trying to use the #include with all of its 
pathing issues).  You can pull your template content from a file, a request to 
another server, etc.

If you are sick of having to re-deploy templates every time that you need to 
make a simple form change this can be a very handy trick.  It can also allow 
the artsy, non-programmer types (in a mixed development environment) modifiable 
access to web page content without worrying about them blasting the formatting 
logic within your templates.  You can put the guts of your pages on servers 
(other than the app server) and let others worry about maintaining it.

Tony O.

-----Original Message-----
From: Tony Oslund [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 29, 2006 11:35 AM
To: Turbine Users List
Subject: RE: Re: Expression evaluation inside a template?

You can indeed do what you are asking, I am currently doing this.
Just build an object or a "tool" with the following method.

Here is what I put into my velocity macro to test it...

$vsesstool is a tool that I created (see the docs...)

Tony

#set ($myExpr =  '$row.getColumn("pl_product_line_description")')
$myExpr
$vsesstool.evalExpression($data,$myExpr)

public static String evalExpression (RunData data, String expression) {

                Context context = TurbineVelocity.getContext(data);

                StringBuffer content = new StringBuffer();

                BufferedReader in = null;

                try {

                        /* this approach makes use of the oreilly.jar file */
                        in = new BufferedReader(new StringReader(expression));
                        StringWriter writer = new StringWriter();
                        Velocity.evaluate(context, writer, "INFO", in);
                        content.append(writer.toString());

                }
                catch (Exception e) {
                        // something went wrong
                }
                finally {
                        if (in != null) {
                                try {
                                        in.close();
                                }
                                catch (Exception e) {
                                        // something else went wrong
                                }
                        }
                }

                return content.toString();

        }
-----Original Message-----
From: news [mailto:[EMAIL PROTECTED] On Behalf Of BoD
Sent: Wednesday, March 29, 2006 11:15 AM
To: [email protected]
Subject: Re: Expression evaluation inside a template?

Thank you for your answer.

I'm not sure I was completely clear so I'll give a more precise example of what I'm trying to do.

Let's say I have a tool that has a method createObject().
This method returns an object that has a getProp() method returning "hello". The tool also has a method getExpression() returning the string "$obj.getProp()".

I'm trying to make a velocimacro that given this:

#set ($obj = $myTool.createObject())
#set ($expr = $myTool.getExpression())
#myMacro($expr)

Would generate this:

hello


So I would need something like eval($expr) that would evaluate the content of $expr which is $obj.getProp(). Obj being declared before it would call the method and return "hello".

I have no idea on how to create such a macro or if it is possible.

Thank you very much for your help.

BoD



Tony Oslund a écrit :

If you read the Velocity docs you will find that it encourages you to
access methods on your objects rather than trying to access a property
directly.

Try doing $x.getValue(), or $y.doSomething() rather than attempting to
do x.myalue.

Along the same lines, if you are tempted to use arrays, use an ArrayList
with Velocity instead.

It is possible to access a method on an object within a Velocity
template that in turn returns the contents of another velocity template,
and so on and so on.  I currently use this to handle language
translation, managed content, and also reusable sections of web pages.

If you are truly looking for expression evaluation.  There is an open
source java library called beanshell.  It allows you to eval a java
expression.  I have used it behind the scenes to perform Boolean
evaluation of string expressions like {X} or {Y} and not {Z}, etc.  It
works very well.

-----Original Message-----
From: news [mailto:[EMAIL PROTECTED] On Behalf Of BoD
Sent: Wednesday, March 29, 2006 8:56 AM
To: [email protected]
Subject: Expression evaluation inside a template?

Hi!

In a template, is there a way to evaluate a Velocity expression inside a

variable ?
For example, let's say I have set in a Java screen a variable "expr" with the value "$myObj.prop" in the context.

Now I'd like to evaluate the expression represented by $expr when I'm in

the template. Is there any way to do that, something like eval($expr)?

Thank you very much for your help!

BoD


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to