type safe templating

Previous Tutorial contents Next

Jamon Tutorial - 2. Dynamic Content

Without the ability to compute text to display, Jamon would have no more capability than HTML. Fortunately, Jamon has an expressive structure to specify what gets returned to the user.

Jamon allows the inclusion of any Java code in the template. Through the use of the following tags, Java can be used process data or to generate text to return from the template.

Tag Action
<% "<%java> ... " %>
Everything between <% "<%java>" %> and <% "" %> is treated as Java code
<% "<%java [Java code] %>" %>
Everything between <% "<%java " %> and <% "%>" %> is treated as Java code
<% "<% [java expression] %>" %>
The Java code between the <% and %> is executed and the result is converted to a string and included inline with the text generated by the template. Note that there must be whitespace between <% and start of the Java code.

For small amounts of Java code, using % escaping is useful.


<%java String dayOfWeek = new java.text.SimpleDateFormat("EEEE").format(new java.util.Date()); %>

For larger chunks of Java code, using <%java> ... </%java> tags may be preferable:


<%java>
  String[] forecasts = new String[] {
    "partly cloudy", "mostly cloudy", "sunny", "rain", "snow" };
  int whichForecast = (int) (Math.random() * 5);
  String forecast = forecasts[whichForecast];
</%java>

In order to include data computed by Java in the text returned by the template, you must use the emit operator. This is the <% ... %> tag. :


Welcome to the free weather site.
The weather for <% dayOfWeek %> is <% forecast %>.
To get a better forecast, please sign up for our premium service.

Jamon also has the following tags to help with flow control; they behave analogously to their java counterparts.

<%while conditional>...</%while>
<%for init; conditional; update %>...</%for>
<%for Type variable: iterable>...</%for>
<%if condition1 %>..<%elseif condition2 %>...<%else>....</%if>

For example:


<%if forecast.equals("sunny") %>Go to the beach!<%else>Stay home and read a book!</%if>
Our premium service can forecast
<%for int i = 1; i < 5; i++ %><% i %>, </%for>5 and 6 days ahead.  Check it out!

Note: Around here, the weather is always one of:
<%for String possibility : forecasts %>  <% possibility %>
</%for>

Creating a template with dynamic content.

  1. Putting this all together in a Jamon template produces TimeAndTempTemplate.jamon:
    <%java String dayOfWeek = new java.text.SimpleDateFormat("EEEE").format(new java.util.Date()); %>
    <%java>
      String[] forecasts = new String[] {
        "partly cloudy", "mostly cloudy", "sunny", "rain", "snow" };
      int whichForecast = (int) (Math.random() * 5);
      String forecast = forecasts[whichForecast];
    </%java>
    Welcome to the free weather site.
    The weather for <% dayOfWeek %> is <% forecast %>.
    To get a better forecast, please sign up for our premium service.
    <%if forecast.equals("sunny") %>Go to the beach!<%else>Stay home and read a book!</%if>
    Our premium service can forecast
    <%for int i = 1; i < 5; i++ %><% i %>, </%for>5 and 6 days ahead.  Check it out!
    
    Note: Around here, the weather is always one of:
    <%for String possibility : forecasts %>  <% possibility %>
    </%for>
    
  2. To call the template, create a java class such as: TimeAndTempTut2.java:
    import java.io.OutputStreamWriter;
    
    public class TimeAndTempTut2 {
      public static void main(String[] argv) throws Exception {
        new TimeAndTempTemplate().render(new OutputStreamWriter(System.out));
      }
    }
    
  3. Set your classpath:
    Windows C:\JAMONTMP> SET CLASSPATH=.;\path\to\jamon-runtime.jar;\path\to\jamon-api.jar;\path\to\jamon-processor.jar
    Unix (sh, bash, zsh, ksh) $ export CLASSPATH=.:/path/to/jamon-runtime.jar:/path/to/jamon-api.jar:/path/to/jamon-processor.jar
    Unix (csh, tcsh) % setenv CLASSPATH=.:/path/to/jamon-runtime.jar:/path/to/jamon-api.jar:/path/to/jamon-processor.jar
  4. Process the template:
    java org.jamon.TemplateProcessor --destDir=. TimeAndTemplateTemplate
  5. Compile everything:
    javac TimeAndTempTut2.java TimeAndTempTemplate*.java
  6. Run it:
    java TimeAndTempTut2
  7. You should see:
    Welcome to the free weather site.
    The weather for Wednesday is sunny.
    To get a better forecast, please sign up for our premium service.
    Go to the beach!
    Our premium service can forecast
    1, 2, 3, 4, 5 and 6 days ahead.  Check it out!
    
    Note: Around here, the weather is always one of:
      partly cloudy
      mostly cloudy
      sunny
      rain
      snow
    
    

Previous Tutorial contents Next