type safe templating

Previous Tutorial contents Next

Jamon Tutorial - 6. Calling Another Template From a Template

Jamon templates can call other templates. The semantics are exactly the same as a method call in Java. The only difference is the syntax.

In Jamon, a template calls another template with the <& templateName ; arguments &> tag. (Notice that the tag delimeters are <& and &> -- not <% and %>.) Some examples of calling templates from a template are shown below.

  <%import>
    java.util.Date;
  </%import>

  <& header &>
  <& /org/foo/weather/ForecastTemplate; time = new Date();
     message = "Thank you for using our wearther service!" &>
  <& /com/tripleclick/ads/Advertisement &>
  <& footer &>
This template calls the header and footer templates from the same package (directory) as this template. It also calls the ForcastTemplate in the org.foo.weather package and the Advertisement template in the com.tripleclick.ads package. The call to the ForecastTemplate provides arguments to the call. These arguments may be required or they may be optional; there is no difference in how required and optional argument values are provided by the caller in Jamon.

Arguments to template calls are assigned by name. The name of the argument is the name specified in the <%args> ... </%args> tag of the called template. The name should be followed by => and then the value of the argument. The value is evaluated as Java code, so it may be any Java expression. Multiple arguments should be separated by semicolons.

An Example

The template below, JamonCallerTemplate.jamon, outputs several rows of names and telephone numbers.
<%args>
  String[] names;
  String[] phoneNumbers;
</%args>
<table>
<%for int i=0; i<names.length; i++ %>
  <tr>
    <td><% names[i] %></td>
    <td><& StdPhoneNumberFormat; number = phoneNumbers[i].trim();
           separator = "-" &></td>
  </tr>
</%for>
</table>
It calls the template StdPhoneNumberFormat.jamon (below) to format the telephone number into a specified format. The separator argument to the StdPhoneNumberFormat template is optional, so the JamonCallerTemplate only has to provide a value to override the default.
<%args>
  String number;
  String separator = ".";
</%args>\
<%if number.length() == 7 %>\
<% number.substring( 0, 3 ) %><% separator %><% number.substring( 3 ) %>\
<%elseif number.length() == 10 %>\
<% number.substring( 0, 3 ) %><% separator %>\
<% number.substring( 3, 6 ) %><% separator %><% number.substring( 6 ) %>\
<%else><% number %></%if>
Notice that the StdPhoneNumberFormat template adds backslashes, '\', at the end of each line that outputs text (the non-java code lines). The backslash at the end of the line escapes the new-line on that line. Jamon outputs text exactly as it appears in the template, including new-lines. To prevent new-lines from being output, each Jamon line must end with a backslash.
The top level template, JamonCallerTemplate is called from the Java class JamonCallerTut6.java:
import java.io.OutputStreamWriter;

public class JamonCallerTut6 {
  public static void main(String[] argv) throws Exception {
    String[] names = new String[] {
      "John Public", "Mary Private", "Lee Protected"};
    String[] phoneNumbers = new String[] {
      "5550324", "4135559232", "4135551212" };
    new JamonCallerTemplate()
      .render(new OutputStreamWriter(System.out), names, phoneNumbers);
  }
}
  • To process and run the template, execute the following commands:
        export CLASSPATH=.:/path/to/jamon-runtime.jar:/path/to/jamon-api.jar:/path/to/jamon-processor.jar
        java org.jamon.TemplateProcessor --destDir=. JamonCallerTemplate SpacerTemplate
        javac JamonCallerTut6.java JamonCallerTemplate*.java SpacerTemplate*.java
        java JamonCallerTut6
    
  • You should see:
    <table>
    
      <tr>
        <td>John Public</td>
        <td>555-0324</td>
      </tr>
    
      <tr>
        <td>Mary Private</td>
        <td>413-555-9232</td>
      </tr>
    
      <tr>
        <td>Lee Protected</td>
        <td>413-555-1212</td>
      </tr>
    
    </table>