SourceForge.net Logo

JLoom Logo JLoom


Overview

Introduction

Commands

Hello World Examples

Installation / Usage

Eclipse Plugin

Download

Overview

Introduction

Commands

Simple Template Mechanisms

Think about the following problem: You have to generate a text document, for example HTML, XML, source-code, email, or documentation.

A template mechanism helps you to add dynamic content. Imagine that the produced document slightly changes every time you generate it. For example, in an email the salutation could depend on the receiver of the mail. This is called dynamic content, by contrast the fixed text is called static content.

Generally a template consists of the static content plus special tags at places where dynamic content has to be inserted. The following shows an example template for an email.

email.template
Hello Mr. <%= name %>,

your account will expire on <%= date %>.
Please login on our homepage and don't hesitate to click our banners.

Thank you!
<%= adminName %>

This template uses the syntax <%= variable %> to insert dynamic content. The syntax is the same in JSP and JLoom where these elements are called Expressions, because they could not only contain String variables but Java-expressions which resolve to a String, for example you could call methods on an object: <%= user.getFirstName() + " " + user.getLastName() %>


JSP-like Template Mechanisms

Such mechanisms take two steps to generate the document:
The first step is called translation. In this step the template is translated to a Java class, called generator class.
The second step is called generation. In this step the generator class is used to generate the document.

This is the clue of these template mechanisms because it allows to write Java code in the template. Besides the translation has to be done only once and any subsequent generation is extremely fast.
In JSP and JLoom these code blocks are called scriptlets and have the following syntax:
<% /*Java-code*/ %>

This allows to use all the powerful features of the Java language, e.g. conditional statements and loops (*). And people who already know Java, don't have to learn a special template syntax and operators, expression rules, assignment rules, etc. Furthermore, enhancements of the underlying programming language (Java) are immediately available, e.g. Generics of Java 1.5.
(*) JLoom lets you use this power in a structured way - each template is an encapsulated parameterized module.

A template for our email example could look like this:
email-for.template
Hello Mr. <%= name %>,

you have the following items in your shopping basket:
<%
Item[] items = getShoppingItems();
for (int i=0; i<items.lenght; i++) {
  %
>

  <%= items[i].getName() %>
  <%
}
%
>


Happy shopping!

The syntax of this template is the same as the JSP or JLoom syntax, more precisely it could be a code fragment of a JLoom template. This applies also for the next example, which shows a conditional statement:

email-if.template
Hello Mr. <%= name %>,

<%
if (hasCredit) {
  %
>

  Happy shopping!
  <%
}
else {
  %
>

  Have a nice day!
  <%
}
%
>


JLoom

The email-if.template would look like this in JLoom:

EmailIf.jloom
<%@ jloom template /%>

<%@ main (String name, boolean hasCredit) %>
Hello Mr. <%= name %>,
<%
if (hasCredit) {
  %
>

  <%-- credit is ok --%>
  Happy shopping!
  <%
}
else {
  %
>

  Have a nice day!
  <%
}
%
>

<%/ main %>

The name of the template changed, because this name will be the prefix of a Java classname and hyphens are not allowed in Java names. Also it is better to start the name with an uppercase letter (Java naming convention), but this is not required.

You may have noticed the two new tags, which start with <%@, these are commands and are explained in the Commands.
Another new tag is <%-- credit is ok --%>, this is a comment - the same syntax as in JSP. Comments will just be ignored. Because the scriptlet is self-explanatory, this is a good example for a bad comment.

This template will be translated into the following generator class:
(shown just for demonstration purpose - you don't have to worry about this code because JLoom creates it for you)
EmailIf_JLoom.java
/* JLoom 1.4.0 generator class */

/* AUTO-GENERATED */
/* Do not edit! Changes will be overwritten! */

package net.gereon.jloom.homepage.examples;

import java.io.*;
import net.gereon.jloom.core.*;
import net.gereon.jloom.util.*;
import static net.gereon.jloom.util.GenerationTools.*;



public class EmailIf_JLoom extends net.gereon.jloom.core.GeneratorBase {


  public void generateMain(GenerationContext context, String name, boolean hasCredit) throws IOException {
    print(context, "Hello Mr. ");
    print(context, "" + (name));
    println(context, ",");
    if (hasCredit) {
    println(context, "  Happy shopping!");
    }
    else {
    println(context, "  Have a nice day!");
    }
  }
}

/* TRANSLATION MAPPING:
[26 287 c] [70 472 c] [82 506 c] [93 539 c] [96 566 c]
[100 570 s] [148 587 c] [171 630 c] [175 634 s] [178 640 s]
[190 647 c] [214 691 c] [218 695 s] [223 697 c] */

JLoom also defines commands for loops and if-statements, which can make your template code a bit more readable. Furthermore they make use of JLoom commands for indention, which result in correct indention of blocks in the generator Java code (whereas the if-block above is not indented correctly), which can be helpful for debugging. By the way, this is a big advantage of such template mechanisms, you can look at the generator code and immediately see how your template was interpreted - extremely helpful in the real world!
In Hello World Examples you can find a more detailed example.



  Generated by JLoom LogoJLoom November 29, 2007   •   You can view the template here: IntroductionPage  
SourceForge.net Logo