velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Terry Steichen" <te...@net-frame.com>
Subject Re: Variable method name in a macro?
Date Mon, 07 Oct 2002 23:45:54 GMT
Chris,

Just to be very clear: Velocity does *not* retrieve properties, public or
not.  It will *only* invoke public methods.  And, using Reflection, if
'propertyName' refers to a method, the 'getter' method would have to be
'getpropertyName()'.

Maybe you could just confirm that, and then let's see about additional
questions. ( I make extensive use of all kinds of tools, with all kinds of
parameters passed to them.  So I know it works.)

Regards,

Terry

PS: And regardless of the above, if you want something like this: #set($xxx=
$yyy$zzz),
I believe you should restate it as this: #set($xxx= "$yyy$zzz").
Furthermore, I find that occasionally when I string varbs together, the
parser gets confused.  If that were to have happened in the above example,
what I'd try would be this: #set($xxx= "${yyy}${zzz}").

----- Original Message -----
From: "Chris Miller" <chris@swebtec.com>
To: <velocity-user@jakarta.apache.org>
Sent: Monday, October 07, 2002 6:41 PM
Subject: Re: Variable method name in a macro?


> 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
> the
> > '\' 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
> dynamically
> 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)
> $tool.eval($evalString)
>
> So, what's the trick? Any help greatly appreciated!! :-)
>
>
>
>
>
> --
> To unsubscribe, e-mail:
<mailto:velocity-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
<mailto:velocity-user-help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


Mime
View raw message