Click here to Skip to main content
12,762,900 members (39,277 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


38 bookmarked
Posted 19 Aug 2008

Simple Template Engine

, 19 Aug 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Generate source code and database scripts for any language or platform.


Code generation is a fundamental aspect of flexible, rapid application development. There are many open source and commercial solutions available, such as Microsoft T4 or CodeSmith Tools, but often times, these tools are targeted at specific technologies or platforms. With a little legwork, you can leverage template processing libraries in modern scripting languages such as Ruby or Python to create powerful, open-ended template engines. You can use these template engines with any language or platform to generate source code, database scripts, configuration files, or any other text-based file.


This particular solution uses the open source Ruby language to process templates, given a simple, flexible XML schema representing your domain model. Ruby comes preconfigured on OS X systems, is easily installed through package managers on most popular Linux distributions, and may be installed on Windows platforms using the Ruby One-Click installer found at

Using the code

This example will use the following trivial domain schema describing a library catalog:

<Library catalog="MyCatalog">

    <enumeration name="Category">

    <enumeration name="Format">

    <object name="Media">
        <property name="ID" type="string" />
        <property name="Title" type="string" />
        <property name="Category" type="Category" />
        <property name="Format" type="Format" />
        <property name="Author" type="string" />
        <property name="ReleaseDate" type="DateTime" />

    <object name="Author">
        <property name="ID" type="string"/>
        <property name="First" type="string"/>
        <property name="Last" type="string"/>


The following template file Enumerations.erb could be used to generate C# enumerations from the library domain specification:

namespace <%= catalog %>

Running the command ruby generator.rb library.xml Enumerations.erb produces the following output:

namespace MyCatalog

    public enum Category

    public enum Format


The following template file Objects.erb can be used to generate simple C# domain objects from the library domain specification:

using System;

namespace <%= catalog %>

Running the command ruby generator.rb library.xml Objects.erb produces the following output:

using System;

namespace MyCatalog

    public partial class Media
        string ID { get; set; }
        string Title { get; set; }
        Category Category { get; set; }
        Format Format { get; set; }
        string Author { get; set; }
        DateTime ReleaseDate { get; set; }
        public object Clone()
            Media copy = new Media();
            copy.ID = ID;
            copy.Title = Title;
            copy.Category = Category;
            copy.Format = Format;
            copy.Author = Author;
            copy.ReleaseDate = ReleaseDate;
            return copy;

    public partial class Author
        string ID { get; set; }
        string First { get; set; }
        string Last { get; set; }
        public object Clone()
            Author copy = new Author();
            copy.ID = ID;
            copy.First = First;
            copy.Last = Last;
            return copy;


If you need to add or modify domain objects, it is simply a matter of updating your XML schema. With the proper templates, you will no longer need to maintain your persistence layer or any other boilerplate code, and any bug fixes or modifications you make to your templates will be immediately available across all of your domain objects. Ultimately, you will spend less time maintaining your plumbing code, and more time doing real development.

Having your domain specification under revision control opens up some more possibilities. You will have a database backup and versioning scheme that corresponds with your repository revision (and possibly build numbers). You could reconstruct your database tables from any point in your repository. You could compare two versions of your schema and generate database rollback and change scripts. Setting up a database for a new developer will be a trivial task.

Points of interest

The Ruby script driving this template engine is very short. I have trimmed out some functionality for the sake of demonstration, but you will have access to the entire Ruby language within the engine as well as your templates, allowing you to extend the engine any way you want.

I've included another (trivial and poorly designed) domain specification to demonstrate the usage of domain specific XML tags. You can use any tag name you want, and specify any attribute you want. One requirement is that the name attribute must be present in any complex element. XML Text fields must also not be used for anything other than representing simple lists (as demonstrated by the following ingredient elements).

<kingdom domain="Animal"/>

    <animal name="Duck" species="Bird">
        <recipe name="Bacon Wrapped Duck" difficulty="Medium">
            <ingredient>10 duck breasts</ingredient>
            <ingredient>3 lbs peppered bacon</ingredient>
            <ingredient>2 jars banana pepper rings</ingredient>
            <ingredient>Italian dressing</ingredient>
        <recipe name="Plain Duck" difficulty="Easy"/>
        <feature name="Feet" value="Webbed" color="Yellow"/>
        <feature name="Tasty" value="true"/>


The following template snippet would allow you to iterate through your recipes:

<%- animals.each do |animal| -%>
    <%- do |recipe| -%>


    <%- end -%>
<%- end -%>

If there is enough interest, I could throw together a more practical solution demonstrating schema validation, nullable data types, multiple languages, inheritance, persistence, and build integration. Feel free to e-mail me at with any questions or comments.

Helpful links


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralUndocumented functionality. Pin
Andy Hass20-Aug-08 18:18
memberAndy Hass20-Aug-08 18:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 19 Aug 2008
Article Copyright 2008 by baker.alex
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid