Remember, Velocity is a template engine for rendering strings, it's
not a scripting language. So things get ugly when you are
scripting-ish stuff. #evaluate does not "return" any values, it
renders the string sent for evaluation. You can use it to do what you
want by embedding a #set into the string you are having #evaluate
render. Also, remember that double quoted strings are interpolated
(#set($d="$") is a syntax error) and single quoted ones (#set($d='$')
is ok) are not.
#for ($panel in $Panels)
#set($selectName = ${panel.CamelCase}+'SortColumn' ) ## The names here are
correct
#set($dynamicProperty = '$preferences.'+$selectName)
<tr>
<td>$panel</td>
<td>
<select name=”$selectName”>
#for($option in $OrderByColumn)
#evaluate( '#set( $preferred = '+$dynamicProperty+' )' )
<option value=”$option” #if($option == $preferred)
selected=”selected” #end>$option</option>
#end
</td>
</tr>
#end
However, it would probably be cleaner to put the context into itself:
context.put("context", context)
Then you can do it like this:
#for ($panel in $Panels)
#set($selectName = ${panel.CamelCase}+'SortColumn' ) ## The names here are
correct
#set($dynamicProperty = '$preferences.'+$selectName)
<tr>
<td>$panel</td>
<td>
<select name=”$selectName”>
#for($option in $OrderByColumn)
<option value=”$option” #if($option ==
$context.get($dynamicProperty)) selected=”selected”
#end>$option</option>
#end
</td>
</tr>
#end
Which should perform better than using #evaluate.
On Mon, Jun 6, 2011 at 11:35 AM, Scott Gentry <[email protected]> wrote:
> Is it possible to dynamically access properties by using #evaluate? I
> apologize in advance for the length, but most of this is just example code
> to fully illustrate my issue.
>
> I have a preferences class which looks like this:
>
> public class DefaultUserPreferences implements Preferences {
> //Getters and setters left off for “brevity…”
> private Panel defaultPanel;
> private OrderByColumn mostActiveSortOrder;
> private OrderByColumn recentlyModifiedAccountsSortColumn;
> }
>
> Each of these types are simply a custom enum.
>
> public enum OrderByColumn {
> NAME,
> LAST_ACTIVITY,
> GROUP
> }
>
> public enum Panel {
> MOST_ACTIVE,
> RECENTLY_MODIFIED;
>
> public String getCamelCase() {
> String[] words = StringUtils.split(this.name(), “_”);
> String rval = StringUtils.EMPTY;
> if (words != null && words.length >= 1) {
> rval = StringUtils.lowerCase(words[0]);
> for(int i = 1; I < words.length; i++) {
> rval += StringUtils.capitalize(words[i].toLowerCase());
> }
> }
> return rval;
> }
> }
>
> Below is a snippet of how I’d like to display the preferences to the users,
> but I can’t seem to get the getter to be called (I get the following if I
> evaluate it to get text:
> test.core.model.entities.DefaultUserPreferences@596fde80.mostActiveSortOrder
> )
>
> #for ($panel in $Panels)
> #set($selectName = ${panel.CamelCase}SortColumn) ## The names here are
> correct
> #set($dynamicProperty = $preferences.$selectName)
> <tr>
> <td>$panel</td>
> <td>
> <select name=”$selectName”>
> #for($option in $OrderByColumn)
> <option value=”$option” #if($option ==
> #evaluate($dynamicProperty) selected=”selected” #end>$option</option>
> #end
> </td>
> </tr>
> #end
>
> However my getter never seems to be called on the preferences. I’ve added
> each of the pieces to the Context, and am not having any issues iterating
> over the Panels, I just can’t seem to get the syntax down to dynamically
> call the getters on properties. Is this possible in 1.7?
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]