Simple Java Template Engine

Template engines are widely used in Web Frameworks, such as Struts, JSF and many other technologies. Apart from classical Web Framework, template engines can be very useful in integration projects. In an actual integration project that deals with a lot of XML data exchange, I discovered the Java Template Engine Library FreeMarker. This Open Source Library is a generic template engine in order to generate any output, such as HTML, XML and any other user defined output based on your given template.

"[...]FreeMarker is designed to be practical for the generation of HTML Web pages, particularly by servlet-based applications following the MVC (Model View Controller) pattern. The idea behind using the MVC pattern for dynamic Web pages is that you separate the designers (HTML authors) from the programmers. Everybody works on what they are good at. Designers can change the appearance of a page without programmers having to change or recompile code, because the application logic (Java programs) and page design (FreeMarker templates) are separated. Templates do not become polluted with complex program fragments. This separation is useful even for projects where the programmer and the HTML page author is the same person, since it helps to keep the application clear and easily maintainable[...]"
I think HTML is one application area of FreeMarker. Consider 3rd party systems providing APIs consuming XML data or their own data structures. Construct their data format in the code is a grubby approach and furthermore the code becomes not maintainable. Using such a library you can manage your data exchange template outside your code and produce the final data by using the template engine. I see such template engines as classical transformers as in an Enterprise Service Bus:

In the above exmple you see, that you can use placeholders in your template files, which will be replaced by the real data when the transformation takes place. FreeMarker provides enhanced constructs such as if statements, loops and other stuff which can be used in your template files.

Template engines are often used in Web Frameworks, but the usage of template engines is also very useful when you must produce specific output for other systems.

Comments

Popular posts from this blog

AGRICULTURAL SCIENTIST RECRUITMENT BOARD

HSC Exam Time Table 2013 Arts, Commerce, Science

What are cookies and its types? Where are cookies used?