velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <ge...@optonline.net>
Subject Re: Run-time dynamic read of Properties
Date Tue, 06 Nov 2001 10:17:54 GMT
On 11/6/01 4:21 AM, "Mooslechner Martin" <martin.mooslechner@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:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


Mime
View raw message