Hello everyone

I have developed another approach to the templating problem.
Most solutions which tried to tackle the approach worked on interepted template level:

https://issues.apache.org/jira/browse/TOMAHAWK-1327

I did another approach the last few weeks
I made an embedded templating compiler for java

I did it due to the inherent speed hits we have by using existing templating solutions and due to the fact that I want to parts of the code in the dojo components project over to templates to increase readability:


What I did was to develop with antlr some mini velocity which can be embedded into java via special comments.
The advantage is, that you simply can embed your templates inline via
a comment flag you can work on valid java code while still having your templating code in one place. It is more or less an inverse scriptlet system to be usable from java.


I will give an example

public class SimpleTest {

    static String helloWorld = "hello world";

    private calledMethod() {

        System.out.println("called function");
    }

    public void emitTemplate() {

        String [] values = new String[5];
        values[0] = "value0";
        values[1] = "value1";
        values[2] = "value2";
        values[3] = "value3";
        values[4] = "value4";



        /*TPL
           #outputop(System.out.println)
           This is a test for a simple embedded template

           $helloWorld

           <table>
           <tbody>

           #each(String, $values)
           </tr><td>$it</td></tr>
           #end

           calling a method

           $this.calledMethod();

           this text should follow after table!

        TPL*/


    }

}


This code is then basically rendered against
the output System.out.println

(other output directives can given via an
#outputop(<output directive>)
command in the template!

I know this approach is not academic due to no MVC
and due to extended control commands like iterations and selections
within the language and the possibility to call external methods
within the template.

The advantage however is, the strings are rendered straight into
output directives almost like handwritten code, by avoiding introspection I can gain similar speeds

(however with the disadvantage that you have to pass a casting datatype
into the #for and #each loops)

But if the community would like it I would merge it into our
utils project next week. Otherwise I can drop it somewhere else.

Werner



Reply via email to