On Tue, Jun 7, 2011 at 9:44 AM, Scott Gentry <[email protected]> wrote: > My apologies, typing on the mobile often makes me less clear. What I was > trying to get at, was if there is a better and faster way to do this (by > using the context versus evaluate, we would prefer to go the context route), > but I am not seeing how to get the values from our preferences object off of > the context. > > What I was getting at is that in our preferences object we have a set > standard of names for the properties. panelASortOrder panelASortColumn, > propBSortOrder, panelBSortColumn. > > I wanted to make the template as easy as possible, so for each panel I need > to display the 2 selects, one for sort order and one for sort solumn. As > the number of preferences and panels will increase, I wanted to be able to > just iterate over the panels, and pull those from the preferences object, > rather then needing to add to the template as well as the Java code in the > future.
hmm. if those properties are static, you can use the FieldTool: http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/FieldTool.html If not, as i suspect, and the property values are strings, then you could using the RenderTool instead of #evaluate, as it returns eval'ed strings, instead of writing them straight to the output: http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/RenderTool.html Last, if you need non-string types from non-static fields, you'll have to write your own support. I would suggest a get(String) method in your preferences class that introspects itself, or something like that. > Is that a little more clear? If not feel free to reply offline. > On Jun 7, 2011 11:10 AM, "Nathan Bubna" <[email protected]> wrote: >> On Tue, Jun 7, 2011 at 5:30 AM, Scott Gentry <[email protected]> wrote: >>> Thanks Nathan, that did the trick. >> >> :) >> >>> We do have the context tool installed, so while I can pull the values out > of >>> the context easily, I don't know if it makes sense in this instance. >>> Wouldn't I need to introspect the class of preferences (and mark all of > my >>> fields as public) or place each value of the preferences onto the context > to >>> pull them that way? >> >> ?? sorry, i had a rough night and can't really figure out what you mean > here. >> >>> Perhaps I'm still missing something? >> >> I don't know. If it already works, what else do you need? >> >>> On Jun 6, 2011 2:58 PM, "Nathan Bubna" <[email protected]> wrote: >>>> 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] >>>> >>> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [email protected] >> For additional commands, e-mail: [email protected] >> > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
