YAWTE Short Tutorial

YAWTE (Yet Another Web Template Engine) is an alternative for generating dynamic content using Servlets. It contains a few template tags used to specify template structure and a tiny Java API for loading templates and generating content.


A template is a text file that may contain:

Following is an example of a template used when generating an HTML file that contains a variable list of tables each containing a variable list of rows:




The row template is specified in the included file:

A child template is generated from the text between the #TEMPLATE() and #END tags and is addressed using the template name specified between the brackets. The template name is used to lookup the template reference in the parent template. The tags for the root template are implied and the root template does not need a name.

Placeholders are specified using a #VAR() tag. The name of the placeholder specified in the brackets is used to lookup the placeholder reference in the parent template.

Files can be include using #INCLUDE(filename) and the semantic is as if the included content would be copied in the including file. The include mechanism does not automatically add a #TEMPLATE tag because the file included may not be a template.

Content Generation

The following code generates an HTML files containing 3 tables each containing 10 rows:
package com.accendia.yawte.test;

import com.accendia.yawte.TemplateManager;
import com.accendia.yawte.Template;
import com.accendia.yawte.Placeholder;

import java.io.FileWriter;
import java.io.IOException;

public class YawteMain
	public static void main(String[] args)
		// Create a template manager that will load, parse and cache templates
		TemplateManager manager = new TemplateManager(null, false);
        Template mainTmpl = null;

		// Load the root template
			mainTmpl = manager.load( "templates/main.html" );
		catch( Exception ex )

		// Get references to child templates and placeholders
		// that will be used to generate content
		Template tableTmpl = mainTmpl.getTemplate( "table" );
		Placeholder head1 = tableTmpl.getPlaceholder( "head1" );
		Placeholder head2 = tableTmpl.getPlaceholder( "head2" );

		Template rowTmpl = tableTmpl.getTemplate( "row" );
		Placeholder val1 = rowTmpl.getPlaceholder( "val1" );
		Placeholder val2 = rowTmpl.getPlaceholder( "val2" );

		// tables iterations
		for( int i=0; i<3; i++ )
			// set the values for the table header
			head1.setContent( "H1_" + i);
			head2.setContent( "H2_" + i);

			// rows iteration
			for( int j=0; j<10; j++ )
				// set values for table cells
				val1.setContent( "VAL_" + i + "-" + j  );
				val2.setContent( "VAL_" + i + "-" + j );

				// generate the content for a row

			// generate the content for a table

		// generate the page content

		// write the content of the root template to file
			FileWriter writer = new FileWriter("page.html");
			mainTmpl.write( writer );
		catch( IOException ex )