Sebastien Dionne

  • Increase font size
  • Default font size
  • Decrease font size

Template Code Generator Part 2 : FreeMarker

E-mail Print PDF

In my previous post, I covered Apache Velocity, Eclipse JET/JET2.
I got a suggestion to look the framework FreeMarker.

FreeMarker look like Velocity. You can even find converters : Velocity -> FreeMarker.
I'll describe in this part how to create the same output but using FreeMarker.

Take a look at the generate method.

SampleFreeMarker.java

  public void generate() {

    try {
      Configuration cfg = new Configuration();
      
      cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);

      Template tpl = cfg.getTemplate("./templates/GalleryTemplateFreeMarker.ftl");
      OutputStreamWriter output = new OutputStreamWriter(System.out);
      
      Map root = new HashMap();
      
      root.put("list", mediaFileList);
      
      tpl.process(root, output);
      
    } catch (Exception e) {
      s_logger.error("generate", e);
    }

  }



Let's compare to the Velocity implementation.

SampleVelocity.java

  public void generate() {

    try {
      Velocity.init();

      VelocityContext context = new VelocityContext();
      context.put("list", mediaFileList);

      Template template = Velocity.getTemplate("./templates/GalleryTemplateVelocity.vm");

      BufferedWriter writer = writer = new BufferedWriter(new OutputStreamWriter(System.out));

      if (template != null)
        template.merge(context, writer);

      /*
       * flush and cleanup
       */

      writer.flush();
      writer.close();
      
    } catch (Exception e) {
      s_logger.error("generate", e);
    }

  }



The main difference is with FreeMarker you need to create a root HashMap and put the objects in it.
With Velocity you put the objects within the context.

Now see the FreeMarker template.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
 <html> 
  <head>
    <!-- include the required JavaScript file --> 
  </head>
 
 <body>
  <div align="center">
    <table width="80%">
<#assign maxItembyRow = 2>
<#assign index = 0>
<#assign count = 0>
<#assign newLine = true>
<#foreach mediaFile in list>
  <#if newLine>
      <tr>
  <#assign newLine = false>
  </#if>
        <td>
            <a href="/${mediaFile.name}" id="player${count}">
            </a>
        </td>
  <#if index < maxItembyRow-1>        
    <#assign index = index + 1>
  <#else>
    <#assign index = 0>
    <#assign newLine = true>
  </#if>
  <#if newLine>
      </tr>
  </#if>
  <#assign count = count + 1>
</#foreach>
    </tr>
  </table>
  </div>
  </body>
</html> 


It's almost the same as Velocity except that the syntax change a little.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
 <html> 
  <head>
    <!-- include the required JavaScript file --> 
  </head>
 
 <body>
  <div align="center">
    <table width="80%">
#set( $maxItembyRow = 2)
#set( $index = 0)
#set( $newLine = true)
#foreach( $mediaFile in $list )
#if( $newLine )
    <tr>
#set( $newLine = false)
#end
#if( $index<$maxItembyRow )
        <td><a href="/$mediaFile.name" id="player$velocityCount"> 
            </a>
        </td>
#set($index = $index+1)
#else
#set($index = 0)
#set( $newLine = true)
#end
#end
    </tr>
  </table>
  </div>
  </body>
</html> 


What I didn't like about FreeMarker is the lack of documentation.
There had good javadoc, but something you need more than that like "real life" samples.
The source code can be downloaded here.

Reddit! Del.icio.us! JoomlaVote! Google! Live! Facebook! Technorati! StumbleUpon! Yahoo! Free social bookmarking plugins and extensions for Joomla! websites!
Comments
Add New Search
wow power leveling  - fgfgfgfg 来祝贺     |123.145.187.xxx |2009-10-11 16:43:36
you can get Wow Power Leveling or wow goldor wow power leveling
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Sunday, 11 January 2009 16:21 )