List Info

Thread: Expression evaluation inside a template?




Expression evaluation inside a template?
user name
2006-03-29 17:35:15
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:newssea.gmane.org] On Behalf Of BoD
Sent: Wednesday, March 29, 2006 11:15 AM
To: turbine-userjakarta.apache.org
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  or  and
not , etc.  It
> works very well.
> 
> -----Original Message-----
> From: news [mailto:newssea.gmane.org] On Behalf Of
BoD
> Sent: Wednesday, March 29, 2006 8:56 AM
> To: turbine-userjakarta.apache.org
> 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: turbine-user-unsubscribejakarta.apache.org
> For additional commands, e-mail: turbine-user-helpjakarta.apache.org


------------------------------------------------------------
---------
To unsubscribe, e-mail: turbine-user-unsubscribejakarta.apache.org
For additional commands, e-mail: turbine-user-helpjakarta.apache.org


------------------------------------------------------------
---------
To unsubscribe, e-mail: turbine-user-unsubscribejakarta.apache.org
For additional commands, e-mail: turbine-user-helpjakarta.apache.org

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )