More projects

rTemplate
high performance templates for Java

Overview

rTemplate is a compiled template language for Java. The template compiler is integrated into Eclipse and also can be accessed as an Ant task. RTemplate is designed for use with code generators but can be used for any text generation purpose.

Motivation

The main motivation for developing the language is that conventional template languages don’t support easy ways to include algorithms (iteration, read a value from a complex model, formatting an output field etc.) into the template. With RTemplate, you have a language that is very close to Java in syntax, and is automatically on-the-fly synchronized to the Java representation. As a result, you can use Eclipse’s full coding assistance power and even refactorings - right in your templates.

Features

Examples

Quick start

  1. Create a new Java project to try rTemplate
  2. Using the context menu on your project convert your project to RTemplate project
  3. Create rtemplate.conf file into the project root directory
  4. Edit the rtemplate.conf file and add javaDir and templateDir properties.
  5. Start editing a Java file. When saved the template version is generated/updated.
  6. Start editing the template file. When saved the Java version is generated/updated.
  7. Move template and Java editors to be shown side by side.
  8. Run the test application as you run a Java application

Template language

To emphasize the simplicity of the library we include the whole source code of the example.

 
			
////package mytemplate;
////
////import java.io.StringWriter;
////import java.util.Date;
////
////public class MyTemplate {
////    public static void main(String[] args) {
////        StringWriter out,
////            rtout,
////            rtcout;
////        out=rtout=rtcout=
////            new StringWriter();
////
#T2#Hello rtemplate World!
////
////        for(int i=0;i<10;++i)
////        {
#T3#This is written 10 times!
////        }
#T2#Current date is #O""+new Date()#
////        System.out.println(out.toString());
////    }
////}
		
		

And the corresponding Java code:

 
package mytemplate;
import java.io.StringWriter;
import java.util.Date;
 
public class MyTemplate {
    public static void main(String[] args) {
        StringWriter out,
            rtout,
            rtcout;
        out=rtout=rtcout=
            new StringWriter();
 
        rtout.write("Hello rtemplate World!\n");
 
        for(int i=0;i<10;++i)
        {
            rtout.write("This is written 10 times!\n");
        }
        rtout.write("Current date is ");
        rtcout.write(""+new Date());
        rtout.write("\n");
        System.out.println(out.toString());
    }
}
        

Escape sequences

The escape sequences used by rtemplate can be modified. The default sequences are practical when you generate Java source code. Other outputs may require different escape sequences. Escape sequences can be set either in rtemplate.conf or in the ant task invocation as simple parameters. Documentation of the tags are here:

 
package hu.rtemplate;
/**
 * Escape sequence constants for template compilation.
 *
 * The constants can be overridden in
 *  * rtemplace.conf file when Eclipse integration is used
 *  * ant task parameters when ant task is used
 * Both Eclipse and ant task parameters have the same name as the fields in
 * this class.
 * @author rizsi
 *
 */
public class TemplateSequences {
    /**
     * Template lines are compiled to Java lines that start with this
     */
    public String jTemplatePre = "rtout.write(\"";
    /**
     * Template lines are compiled to Java lines that end with this
     */
    public String jTemplatePost = "\");";
    /**
     * Template Java argument outputs are compiled to Java lines that start with this
     */
    public String jOutPre = "rtcout.write(";
    /**
     * Template Java argument outputs are compiled to Java lines that end with this
     */
    public String jOutPost = ");";
    /**
     * Java lines that end with this line comment will be included
     * in the template without line break
     */
    public String jLineNonBreakPost = "//NB";
    /**
     * Template line content after this is compiled to a Java source line
     */
    public String tJavaLinePre = "////";
    /**
     * Template Non break Java line tag open. Tag's content is compiled to a Java source
     * line that ends with //NB
     */
    public String tTagNonBreak = "##";
    /**
     * Template set tabulators tag open. Tag's content is an integer.
     * Generated Java source code will be indented with this number of tabulators
     */
    public String tTagTabs = "#T";
    /**
     * Template tag close. All tags included in template are closed with this token
     */
    public String tCloseTag = "#";
    /**
     * Template print tag. Tag's content is a Java expression that is written to output.
     */
    public String tTagPrint = "#O";
}		
		

Availability

SVN

Check out the sources from https://opensource.qgears.hu/svn/trunk/rtemplate/

Eclipse plugin build

Download and install by copying into the dropins folder: hu.rtemplate_0.8.8.jar

License

rTemplate is released under the Apache License v2.0.