Template engine

SourceForge.net Logo

Template engines allow easy separation of visual design from program code.
What this template engine does is nothing new. What makes it different is the way this is achieved and it's availability for different technologies.


Template markup cannot really be called a language, it has no control structures. All control is kept in the program, which uses the engine.

Since this template engine is primarily intended for use in web apps, it is imoprtant that the template markup does not spoil a well-formed HTML document. The markup is extremely simple, only two types of entities exist: Fields and sections.

Fields are placeholders in the template, where text can be inserted from your program. A field may look like this:


A section defines a part of the document, which is treated as a template inside the parent template. To access fields and sections within a section, you must select it. A section may look like this:

	<!-- @@SOMETHING@@ -->
	  a lot of text and @@FIELDS@@ here...
	<!-- @@SOMETHING@@ -->

Notice that section tags are valid HTML comments, which makes it possible to use them anywhere in the HTML code, except for inside other comments. The HTML validator will ignore those tags, allowing your templates to be validated for well-formedness.

The examples on this page how practical use of fields and sections.


At this monent, the examples are the only documentation available except for the source code.

Example 1
Simple example of generating table rows.
PHP example     ASP.NET example

Example 2
A somewhat more complex example which shows the power of nested sections.
PHP example     ASP.NET example


The template engine packages found here are published under the GNU Lesser General Public License version 2.1.

If you are unfamiliar with this license, you may find it at www.gnu.org under Licenses. You may also view a local copy of this license.


C library of template processing functions
This is the most basic implementation, but probably also the most powerfull one.
The library is actually one large C file, which you can include from your programs.
Included in this download are two CGI sample programs and two templates.
Download template4c.zip

Template engine for PHP
This packege contains a loadable extension for PHP.
It is based on the above C library of template procedures, which is included in this package.
For your convenience compiled modules for Windows and Linux are included.
This package comes with a PHP include file, that provides the same functionality as the extension, but without any installation. You just "require" it in your scripts. The extension however, performs better and should be preferred.
Download template4php.zip

Template engine for Java
Java package especially intended for Java servlets and Java Server Pages.
Offers the same semantics as the PHP extension.
Download template4java.zip

Template engine for .NET
This component is written in C# and requires Microsoft .NET framework, freely available from Microsoft. Same semantics as the PHP extension.
Note that it is possible to register this assembly as a COM component and use it from ASP, providing that you have the .NET framework installed.
Download template4dotnet.zip


Loading a template does indeed add some overhead to your program. A simple template like one of the example templates takes less than half of a millisecond to read and scan. More complicated, larger template files will take longer.
Furthermore, adding sections in a loop will take longer than simply setting some fields.

Off course, this overhead is not critical if you use templates for generating static pages, or if your app does not handle a lot of traffic. Actually, this overhead is not overwhelming. Including a file in PHP takes longer than loading a template, to give an example.

You may want to cache a template data structure once it is loaded, and simply clone it for every client request. This is very easy to do in Java servlets and FastCGI programs. However, be aware that changes made to a template file will not be reflected by the cached template data.

About me

My name is Niels Wojciech Tadeusz Andersen, my e-mail address is haj@zhat.dk.

I have developed this template engine out of a need, for use in my own projects. I needed the same solution for PHP, ASP/ASP.NET, C programs and Java, to ensure template file compatibility and because I find this way of working with templates easy and potent.
I use this template engine myself, primarily for my web page zhat.dk, which is almost entirely written in PHP.

If you find bugs, have suggestions, or find a more efficient algorithm, please let me know.

Valid XHTML 1.0!