M2T-JET-FAQ/Are there equivalents to the MVC pattern for JET transformations?

From Eclipsepedia

Jump to: navigation, search

Question

Are there equivalents to the Model-View-Controller (MVC) pattern for JET transformations?

Answer

Here is a basic pattern:

1) templates that generate content correspond to MVC views. These are templates that are referenced from tags such as <ws:file>

Rules for view templates (VT):

 VT1) Avoid doing calculations in the template. Many values may be pre-calculated by using the <c:set> tag to create 'derived' attributes on model elements. Such tags are executed in the controller
 VT2) The most comment tags in view templates should be <c:get>, <c:iterate> and conditionals (<c:if>, <c:choose>, ...)

2) templates that orchestrate the creation of contents correspond to the MVC controller. The main.jet template is the primary controller, although it may delegate to other templates via <c:include>

Rules for controller templates (CT1):

 CT1) Perform any model manipulation and value pre-calculation in the controller and not view templates. This is generally done by traversing the model (c:iterate tags) and by using c:set to calculate values.

3) Models. A JET transformations input is a model. However, it may not be exactly what you need for efficient template execution. You typically may do one or both of the following (in the controler)

a) Calculate values and store their results as annotations on the model via <c:set>. These annotations appear to the JET XPath engine as normal attributes.

b) load or create additional models via tags such as <c:load> and <c:loadContents>.

Other best practice rules:

1) In view templates, replace whole language tokens. That is:

  Don't do:   public int get<c:get select="camelCase($e/@fieldName)"/>();
  Do do: public int <c:get select="$e/@getter"/>();

Of course, there will be a c:set tag something like this in the controller (main.jet):

  <c:set select="$e" name="getter">get<c:get select="camelCase($e/@fieldName)"/></c:set>

2) Try to avoid Java code in templates. If you much do Java code, put most of the code in a utility class, rather than directly in the template.