You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Mooslechner Martin <ma...@siemens.at> on 2001/11/06 10:21:21 UTC

Run-time dynamic read of Properties

Hi,

I have a variabe, say $user, with many properties $user.firstname,
$user.lastname, ...

I know that I can get the values with either $user.firstname or
$user.getFirstname(),
but is there a possibility to specify the property at runtime? 

something like

#set ( $desired_properties = ["firstname", "lastname", "birthdate"] )

#foreach ($property in $desired_properties)
	The property $property equals: $user.$property
#end

but it doesn't work that way.
Is there a way to achieve this with the formal notation? Is there a #parse
similiar directive where I can not only parse files, but also variables
(would it be possible to put "\$user.$property" in a variable, which will be
parsed then)?

I'd appreciate any suggestions,

thank you, 

Martin Mooslechner
martin.mooslechner@siemens.at


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Run-time dynamic read of Properties

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/6/01 4:21 AM, "Mooslechner Martin" <ma...@siemens.at>
wrote:

> Hi,
> 
> I have a variabe, say $user, with many properties $user.firstname,
> $user.lastname, ...
> 
> I know that I can get the values with either $user.firstname or
> $user.getFirstname(),
> but is there a possibility to specify the property at runtime?
> 
> something like
> 
> #set ( $desired_properties = ["firstname", "lastname", "birthdate"] )
> 
> #foreach ($property in $desired_properties)
> The property $property equals: $user.$property
> #end
> 
> but it doesn't work that way.
> Is there a way to achieve this with the formal notation? Is there a #parse
> similiar directive where I can not only parse files, but also variables
> (would it be possible to put "\$user.$property" in a variable, which will be
> parsed then)?
> 
> I'd appreciate any suggestions,

To answer the question, no, there is no 'dynamic' notation.  However, you
have two options :

1) Use a public String get(String) property accesor in your class, a la

#set ( $desired_properties = ["firstname", "lastname", "birthdate"] )
 
 #foreach ($property in $desired_properties)
   The property $property equals: $user.get($property)
 #end


2) You could make your own little tool using the Velocity.evaluate() or
VelocityEngine.evaluate() methods to do dynamic parsing...

I'd go with #1 if you can.  Makes things very obvious in the template.

geir

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>