*Deprecated* See https://wiki.duraspace.org/display/VIVODOC/All+Documentation for current documentation

From Stephen V. Williams, May 29, 2013:

How do the templatemodels map into the freemarker template?

Example:

I'm looking at

/Vitro/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/searchresults/IndividualSearchResults.java
/Vitro/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/searchresults/BaseIndividualSearchResults.java
/VIVO/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/searchresults/IndividualSearchResults.java
Now there are methods in BaseIndividualSearchResults.java for
  • getName()
  • getUri()
  • getProfileUrl()

and I can access these from my freemarker template as

  • ${individual.name}
  • ${individual.uri}
  • ${individual.profileUrl}

However, there is a method in IndividualSearchResults.java for VIVO (not Vitro, both extends BaseIndividualSearchResults.java) called "getPreferredTitle".  I can not access this in my freemarker templates.  At first I just assumed (cause I'm being hacky) that somewhere the get of each method mapped to a property (ie getName() in java mapped to .name in Freemarker).  That doesn't seem quite right so I searched for something that mapped them together and came up with nothing.  Then I started researching Individual which has .name but doesn't have .profileUrl and .uri (that I can tell).  

I'm hunting around here, realize I'm a bit lost and that someone on the list probably understands this better than me.

Thank you!

Stephen

From Jim Blake, May 30, 2013:

Stephen;

As I thought, it's a mixture of Freemarker defaults and local mods.

Every value that is made available to a Freemarker template must implement the TemplateModel interface. At some point, the idea of wrappers was introduced, and automatic wrapping. You put values into the body map, but before they are accessed, Freemarker will wrap them with TemplateModel wrappers.

Check out these descriptions:
http://freemarker.sourceforge.net/docs/pgui_datamodel_objectWrapper.html
http://freemarker.sourceforge.net/docs/pgui_misc_beanwrapper.html

But if you add a value to the body map, and that value already implements the TemplateModel interface, then Freemarker will not add a wrapper to it. And that opens the door to local optimization.

So (working from the "develop" branch as of May 30, 2013)

  • In IndividualResponseBuilder, an instance of Individual is passed into the constructor (line 67). This object was assembled from the data in the model, by an IndividualDAO.
  • In IndividualResponseBuilder.assembleResponse() line 103, the Individual is wrapped in an IndividualTemplateModel. This seems like it would be sufficient for our purposes. However…
  • In line 112, this is wrapped in a ReadOnlyBeansWrapper before being added to the body map. This prevents the Freemarker template from modifying the contents of the IndividualTemplateModel. Of course, since IndividualTemplateModel has no mutator methods, this may be overkill. Or it may simply be vestigial.

Does this help?

Jim


  • No labels