velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <>
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" <>

> 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)

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 Magnusson Jr.                           
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:   <>
For additional commands, e-mail: <>

View raw message