type safe templating

Previous Tutorial contents Next

Jamon Tutorial - 5. More Advanced Jamon Tags

Jamon templates are translated into Java classes and interfaces. Jamon provides several tags to give the template author more control over how declarations in the template get translated into Java constructs.
  1. Jamon comments The Jamon comment tag, <%doc> ... </%doc> causes all text between the begin and end tags to be ignored by the Jamon processor and the Java compiler.
  2. Import statements The example templates shown up to this point have completely specified every Java class not in the java.lang package. In the tutorial section on Templates with arguments the examples contained the declarations
    
    <%args>
      java.util.Date effectiveDate;
      String userName;
      java.math.BigDecimal totalBalance;
    </%args>
    
    

    Jamon provides the <%import> ... </%import> tag to allow the user to import classes for the template.

    
    <%import>
      java.util.Date;
      java.math.BigDecimal;
    </%import>
    <%args>
      Date effectiveDate;
      String userName;
      BigDecimal totalBalance;
    </%args>
    
    
    Static imports can be acheived with the static keyword; for example:
    
    <%import>
      static java.lang.math.abs;
    </%import>
    The difference between 2 and 4 is <% abs(2 - 4) %>.
    
    
  3. Class scope declarations The Java code included in the template by the % and <%java> ... </%java> escapes, as well as that in the <%if...%>, <%elseif...%>, <%for...%>, and <%whilte...%> tags, is translated into statements inside methods on a Java class created from the template.

    For example, the template fragment below

    
    <%java int count = 3; %>
    <%while count-- > 0 %>Hello world!
    </%while>
    
    
    is translated into Java code inside a method that looks something like:
        int count = 3;
        while (count-- > 0) {
          this.write("Hello world!\n");
        }
    
    To add variables or methods to the class which contains these methods, use the <%class> ... </%class> tag. For example, the following Jamon template
    <%class>
      static int pageViews = 1;
    </%class>
    This component has been accessed <% pageViews++ %> times.
    
    generates a Java file looking something like
    public class SomeTemplateImpl
    {
      static int pageViews = 1;
    
      public void render() throws java.io.IOException
      {
        this.write("\nThis component has been accessed ");
        this.writeEscaped(this.valueOf(pageViews++ ));
        this.write(" times.\n\n");
      }
    }
    

    Any Java code that is legal at the class level, including fields, methods, and classes, may be declared in a Jamon template between the <%class> ... </%class> tags.

An Example

The template below, ClassExampleTemplate.jamon, imports three classes, java.util.Date, java.text.DateFormat, and java.text.SimpleDateFormat. It also defines two class static fields and one method. The template code uses all of these fields and method to output the desired text.
<%import>
  java.util.Date;
  java.text.SimpleDateFormat;
  java.text.DateFormat;
</%import>
<%args>
  Date currentTime;
  int count;
</%args>
<%class>
  private static int iterationCount = 0;
  private static DateFormat dateFormat = new SimpleDateFormat();
  private String formatDate(Date date) {
    return dateFormat.format(date);
  }
</%class>

<%for int i = 1; i <= count; ++i %>\
        Hello world! The time is <% formatDate(currentTime) %>.
        This template type has been called <% ++iterationCount %> times.
</%for>

When this template is called by the java code ClassExampleTut5.java:

import java.io.OutputStreamWriter;
import java.util.Date;

public class ClassExampleTut5 {
  public static void main(String[] argv) throws Exception {
    new ClassExampleTemplate()
      .render(new OutputStreamWriter(System.out), new Date(), 3);
  }
}
the output of the template's render method will be
        Hello world! The time is 8/12/09 8:38 PM.
        This template type has been called 1 times.
        Hello world! The time is 8/12/09 8:38 PM.
        This template type has been called 2 times.
        Hello world! The time is 8/12/09 8:38 PM.
        This template type has been called 3 times.

Previous Tutorial contents Next