Quick Start

Overview

TemplAT is a general purpose text file template engine. It allows a Java developer to construct templates of text files (HTML, SQL, etc.) with a simple programming language, and use the TemplAT API to render text files from the templates.

Simple Example

Once you have downloaded the templat.jar file and put it into your classpath, you can make a simple example program to demonstrate how TemplAT works. For example, create a text file called displayBooks.tat; this will be our template.
displayBooks.tat
@template displayBooks(books)@
We have the following books:
@loop b : books.size()@
    @books.get(b).getTitle()@ by @books.get(b).getAuthor()@.
@end loop@
The highlighted regions represent tags that will be parsed by the Templat class. Tags are surrounded by at-signs (@). The template tag defines this file as a template, and specifies its parameters. The loop tag starts a loop block, and identifies the loop counter variable and the number of times the loop block will be expanded. The expressions in the next line will be parsed by the Templat class, and the returned values expanded into the resulting text. The end loop tag ends the loop block.
Then create a Java program (JDK >= 5.0) that renders the template (given a list of books), and prints the results.
PrintBooks.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.templat.Templat;
import net.sourceforge.templat.exception.TemplateLexingException;
import net.sourceforge.templat.exception.TemplateParsingException;

public class PrintBooks
{
    // A simplified structure that represents one book
    public static class Book
    {
        private final String author;
        private final String title;

        public Book(String a, String t) { author = a; title = t; }

        public String getAuthor() { return this.author;}
        public String getTitle() { return this.title;}
    }

    public static void main(String[] arg)
        throws TemplateLexingException, TemplateParsingException, IOException
    {
        // This is our inventory of books:
        List<Book> books = new ArrayList<Book>();
        books.add(new Book("Rudyard Kipling","The Jungle Book"));
        books.add(new Book("Mary Shelley","Frankenstein"));
        books.add(new Book("Oscar Wilde","The Picture of Dorian Gray"));

        // Get our displayBooks template:
        Templat tat = new Templat(PrintBooks.class.getResource("displayBooks.tat"));

        /*
         * Render the template, passing our array of books for
         * the argument, and put the result into the StringBuilder.
         */

        Appendable result = new StringBuilder();
        tat.render(result,books);

        // Print out the result
        System.out.println(result);
    }
}
Compiling and running the program produces the following output:
java -cp .;templat.jar PrintBooks
We have the following books:
    The Jungle Book by Rudyard Kipling.
    Frankenstein by Mary Shelley.
    The Picture of Dorian Gray by Oscar Wilde.
Valid XHTML 1.1