Jump to content

Web technologies -- Laboratory 4 -- 2009-2010 -- info.uvt.ro

From Wikiversity

Templating engines

[edit]

A Templating Engine is software that is designed to process templates and content information to produce output documents. It can be successfully used in Web content generation. It allows separating

Examples include:

  • Velocity (Java)
  • Freemarker (Java)
  • JavaServerPages (Java)
  • Phptal (PHP)
  • Smarty (PHP)
  • ActiveServerPages (.NET)
  • XSLT (XML)

The main advantages of Templating Engines is that they:

  • Enforce the separation of functional responsibility (eg. designer from programmer)
  • Promote division of labor
  • Provide easier syntax
  • Increase security

Templating Engines can be used in:

  • Servlet-based Web applications
  • Java and SQL code generation
  • XML processing and transformation
  • Text processing, such as RTF file generation

Examples

[edit]

Velocity

[edit]

Download from : http://apache.unixteacher.org//velocity/engine/1.7/velocity-1.7.zip (Project website http://velocity.apache.org/download.cgi)

When using Velocity, information is usually retrieved from beans. Consider for example the following code which displays Welcome John to your first Velocity application. Your email address is john@student.com.

import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
 
public class VelocityTest {
	public static void main( String[] args ) throws Exception {
		// Get and initialize an engine
		VelocityEngine ve = new VelocityEngine();
		ve.init();
		// Get the Template
		Template t = ve.getTemplate( "hello.vm" );
		// Create a context and add data
		VelocityContext context = new VelocityContext();
		UserInfo user = new UserInfo("John", "john@student.com");
		context.put("user", user);
		// Render the template into a StringWriter
		StringWriter writer = new StringWriter();
		t.merge( context, writer );
		// Show the result
		System.out.println( writer.toString() ); 
	}
}
 
public class UserInfo {
	private String user, email;
 
	public UserInfo (String name, String email) {
		this.user = name;
		this.email = email;
	}
 
	public String getUser() {
		return this.user;
	}
 
 
	public String getEmail() {
		return this.email;
	}
}

where the hello.vm file looks like:

Welcome $user.user to your first Velocity application. Your email address is $user.email.

IMPORTANT: notice that we do not have to use the exact getters for retrieving the value of the fields. Velocity takes care of that for use by using Java Reflection.

You can also use directives such as:

  • #set()
  • #if()
  • #else
  • #elseif()
  • #end
  • #foreach()
  • #include()
  • #parse()
  • #macro()

Links:

Exercises

[edit]
  • Start from the data in XML format from the previous laboratory. Create beans and/or lists for it and populate them with data directly from within java code. Generate the same HTML code, as in the previous laboratory, using a Velocity template.
  • BONUS: Create a simple web server based based on the client-server exercise from [1] as follows:
    • the client should send an HTTP request on a socket on which the server listens;
    • the server should interpret the request:
      • if the request is valid the response should contain the 200 OK message plus the HTML code generated in the previous exercise;
      • if the request is invalid the response should contain 400 Bad Request message.
    • the client should display at stdout the received HTML if the response's code is 200 and the message Bad request in case of error 400.

The first person that solves the bonus exercise receives a 10 as its first grade.