JLoom Logo JLoom




Hello World Examples

Installation / Usage

Eclipse Plugin




Hello World Examples

The Introduction already described expressions, scriptlets and comments. The JLoom syntax also defines a set of commands. They are similar to JSP directives, but have a slightly different syntax.
A command-tag is characterized by an at-sign:
<%@ command argument1 argument2 ... %>

This is the basic structure of a JLoom template:

<%@ jloom template /%>

<%@ main () %>

Here is the content...

<%/ main %>

Every JLoom template contains at least the jloom- and main-command.

Note the slash in the end of the jloom-tag: /%>
This is similar to XML syntax, such a tag is empty and immediately closed. The main-tag doesn't have this slash, therefore there is an explicit close tag which has the slash in the beginning instead of the at-sign: <%/


This command has to be in the first line of a template. The parameter is the generator type, in this case "template". For a start the only thing you need to know is, that you can write all your templates just with the "template" declaration.

If you declare "macro" as generator type, the template is a special template called macro. Macros are used to define new JLoom commands.
Alternatively you can declare the name of a Java class:
<%@ jloom net.gereon.jloom.homepage.core.PageGenerator /%>
This class is used as superclass of the generator class. "template" and "macro" are in fact aliases for default superclasses.


Within the tags of the main-command, the main template code is defined. The argument of this command declares the parameters of the template. In the example above it has no parameters, therefore the argument is ().

The space between main and () is required, this is consistent with the command syntax:
<%@ command argument1 argument2 ... %>

Braces can surround a command argument (this permits whitespaces inside the argument).
In fact the main command has only one parameter (not to be confused with the parameter(s) of the template - which are defined by the argument of the main-command).

The template can declare arbitrary parameters. For example, this main command declares two parameters, String and int:
<%@ main (String name, int size) %>

Parameters can be any Java type or object. JLoom uses Java as backbone, the syntax is the same as in Java method declarations. This means you can make use of the most recent Java (currently Java 1.5) syntax features, e.g.:
Generics: <%@ main (Collection<String> names) %>
Varargs: <%@ main (String... names) %>


A template can define a tail, which means essentially that it surrounds something instead of just being inserted. This is analog to immediately closed XML tags versus surrounding tags.
The tail-command defines the template code which have to be inserted at the closing exec-command-tag. The tail-command is similar to the main-command but has no parameter:
<%@ tail %> ... <%/ tail %>

For example the hint-boxes (as seen above in the main-command section) in these webpages are defined/generated by the following template:

<%@ jloom template /%>

<%@ main (int width) %>
<table <%@ if (width > 0) %> width="<%= width %><%/ if %>>
<div class="hintOuter" title="Hint">
<div style="padding: 3;">
<img src="/images/hint.png" alt="Hint" border="0">
<div class="hintInner">
<%/ main %>

<%@ tail %>
<%/ tail %>


Within a template, you can execute another template with the exec-command:
<%@ exec Template (...template-arguments...) /%>

The arguments are checked at compile-time. The rules are the same as for Java method calls, as a consequence you can make use of all Java features like String concatenation with the + operator, expressions / calling methods to calculate the argument, or Java 1.5 Autoboxing.

Templates are organized in packages just like Java classes. This means the name of the called template (first argument of the exec-command) - more precisely its generator class - must be in the callers namespace.
If both templates reside in different packages, there are two possible solutions. You can import the generator class of the called template (with the import-command - see further below) or you can use the qualified template-name, e.g.:
<%@ exec net.gereon.jloom.homepage.templates.ExampleCode ("HelloWorld.jloom") /%>

This is an example template which executes the Hint template:

<%@ jloom template /%>

<%@ main () %>

<%@ exec Hint (200) %>
Any sufficiently advanced bug is indistinguishable from a feature.
<%/ exec %>

<%/ main %>

The template which generated this page contains the line
<%@ exec HintUsage () /%>
at this location:
Any sufficiently advanced bug is indistinguishable from a feature.


You can use the import-command to import Java-classes and templates (their generator classes which are Java classes), this is equivalent to Java import statements:
<%@ import net.gereon.jloom.homepage.core.* /%>

It is possible to make static imports (Java 1.5):
<%@ import (static net.gereon.jloom.homepage.util.TranslationTools.*) /%>
The brackets are necessary because the import command accepts only one argument which is inserted in the generator class.

incIndent     decIndent

These commands are especially useful for code generation. They can be used to create block indention to make the generated code more readable.
incIndent increases the current indent, decIndent decreases it. Which characters are used for indention depends on the GenerationContext implementation. If you use SimpleGenerationContext, you can define the indention characters in the constructor call (see Hello World Examples).
All build-in JLoom macros make use of these commands - therefore the generator classes have correct indention. For example, the "for" macro increases indent in its main-part and decreases indent in its tail-part:
<%@ main (String arg) %> for (<%= arg %>) { <%@ incIndent /%> <%/ main %> <%@ tail %> <%@ decIndent /%>} <%/ tail %>

if     else     elseIf     for     while     doWhile

These commands can be used to create conditional statements and loops. Remember that you can use scriptlets to create arbitrary Java code - so these commands are just an alternative to scriptlets (which is also true for some other commands).
The usage of these commands can make the template more readable. Furthermore these commands create correct indention which makes the generator class more readable.
You will find usage examples in the "Hello Big World" template - see Hello World Examples.

  Generated by JLoom LogoJLoom November 29, 2007   •   You can view the template here: CommandsPage Logo