velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Miller" <>
Subject Re: Variable method name in a macro?
Date Mon, 07 Oct 2002 22:41:03 GMT
Hi Terry,

> Is 'propertyName' the name of a public method, or is it a property?

It's a property, declared with public getters and setters.

> Is this VTL code in a macro or in the main template body?

I've tried both without success. Ideally though for real world use I'll want
it to be in a macro.

> Curiosity: (1) why the single quotes around 'propertyName', and (2) why
> '\' in "\$option..$method"?

1) No reason, bad psuedocode :-)
2) This is where I'm really having problems I suspect. I was trying to
escape the $ sign to prevent evaluation. Let's look at an example. Suppose I
have a book bean, with properties title, author, etc. I'd like to be able to
perform the equivalent of this java code (assume the book object is already
in the context):

String propertyName = "title";        // In reality this is retrieved
String evalString = "$book." + propertyName;
String title = tool.eval(evalString);

The big problem I seem to be having is how to pass the string "$book.title"
into the eval method - the eval method itself seems to work fine. How do I
escape the $ character? Everything I have tried has failed either with a
lexical error, or it does not evaluate as I would like.

eg, I would have thought this would work, but it doesn't:

#set($propertyName = "title")
#set($objName = '$book.')
#set($evalString = $objName$propertyName)

So, what's the trick? Any help greatly appreciated!! :-)

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message