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
- Compilation of templates is bi-directional and on-the-fly: template to Java and Java to template.
- Text output code can be edited in Java and template views of the IDE, side by side.
- There is no runtime dependency.
- Can be easily integrated into any framework (Servlet, OSGi, Application server etc.)
- Compile time dependency is a 51K jar (source included) without 3rd party dependencies.
- Learning curve is 10 minutes. If you know Java you also know RTemplate.
- No need to learn thousands of tags. Only uses 6 simple escape sequences.
- Could be extended to use with languages other than Java.
Examples
Quick start
- Create a new Java project to try rTemplate
- Using the context menu on your project convert your project to RTemplate project
- Create rtemplate.conf file into the project root directory
- Edit the rtemplate.conf file and add javaDir and templateDir properties.
- Start editing a Java file. When saved the template version is generated/updated.
- Start editing the template file. When saved the Java version is generated/updated.
- Move template and Java editors to be shown side by side.
- 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.