Template Engine for Android Development

Posted: July 24, 2009 in Android, Java, Mobile
Tags:
Content (from a database), and "presentat...
Image via Wikipedia

Because I develop Android Applications that often use both Java and Webview I have been searching for a template engine that could work equally with java source, html, and  even sql. Something easy as:

package com.${project.company}.${project.name}.domain;
/*$if{project.framework != project.name}*/
import com.${project.company}.${project.framework}.domain.BaseEntity;
/*$end*/
public class ${entity.name} extends BaseEntity {
  /*$for{field : entity.fields}*/
  private ${field.type} ${field.name};

  public ${field.type} get${field.name.capitalize}() {
    return ${field.name};
  }

  public void set${field.name.capitalize}(${field.type} ${field.name}) {
    this.${field.name} = ${field.name};
  }
  /*$end*/
}

or html-wise:

<html>
    <body>
        <!--$if{users != null && users.size > 0}-->
<table border="1">
            <!--$for{user : users}-->
<tr>
<td><!--$output{for.index + 1}-->1<!--$end--></td>
<td><!--$output{user.name}-->james<!--$end--></td>
<td><!--$output{user.coins}-->2.00<!--$end--></td>
</tr>
<!--$end--></table>
<!--$end-->
    </body>
</html>

Notice because the commands are enclosed by comments will work in all IDEs. How to target to automate? Use local.properties as we already assign package name, application name, activity name, and sdk target when we create the project. This could be expanded to say mapview=true, webview=true, and etc in local.properties than your build system would auto-generate at the init target execution generation of those starting class files using the template engine.

The examples shown above are form the template engine called Common Template Engine. Nice ANT task to run right from ANT and very smal syntax and command set to learn unlike either Velocity or Freemarker.

Reblog this post [with Zemanta]
Advertisements
Comments
  1. HonPlorerok says:

    I really enjoyed reading your blogpost, keep on making such exciting articles!

  2. Felix says:

    After failing with both FreeMarker and Closure Templates, your post saved my bacon. Common Template Engine looks indeed very nice. Note that there’s also Jtpl for simpler tasks — much more lightweight.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s