Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Take the fast track to text generation

Create text content with template engines and save time and frustration

  • Print
  • Feedback
Most programs need to create some sort of text output, like email messages, HTML files, or console output. But because computers speak only in binary, programmers must make their software create intelligible text. In this article, I show you how using template engines can save you time when creating text output. You will learn the advantages of templates and find out how to create effective templates in different scenarios. Say goodbye to System.println()!

While programmers can easily write code to output text messages (after all, that's the first thing you learn in the Hello World example), often they're not the best people for composing messages in the first place. That is why we assign the job to marketing and public relations departments. But message writers unfortunately depend on programmers to complete even the most mundane tasks in terms of program output, which leads to misunderstandings and frustration for both sides.

Take a simple example: a Java program collects some client information from a data source and sends an account statement via email to each of your company's customers. Look at a sample program that accomplishes this task (the full examples are available for download from Resources):

        for (int i=0; i < customers.size(); i++)
        {
            Customer customer = (Customer)customers.get(i);
            
            StringBuffer message = new StringBuffer();
            message.append ("Dear Mr/Mrs ");
            message.append (customer.getLastName());
            message.append ("\n");
            message.append ("\n");
            message.append ("The total on your account is ");
            message.append (customer.getAccountTotal());
            message.append ("\n");
            message.append ("\n");
            message.append ("Regards");
            message.append ("\n");
            message.append ("Widgets and Gadgets Inc.");
            
            // Send Email
            mm.sendMail (customer.getFirstName(), customer.getEmail(), "Account", message.toString());
        }


The example above provides one of the worst ways to send a message. By embedding the message into the program source code, you make it virtually impossible for any nonprogrammer to edit the message without a programmer's help. You also make editing difficult for any programmer who does not know the code. Perhaps, because you foresee this detriment, you write the code below:

    static public final String STR_HELLO="Dear Mr/Mrs ";
    static public final String STR_MESSAGE="The total on your account is ";
    static public final String STR_BEY="Regards\nWidgets and Gadgets Inc.";


The code above doesn't ease the situation much, if at all. A nonprogrammer can hardly be expected to understand the meaning of static or final. Furthermore, code like this is not flexible enough to handle a change in message structure. You might, for example, be required to add more information from the data model to the email message, which would require you to change the email build code and possibly add more static final String objects.

Introduction to templates

Loading the message from a text file would solve some of the problems -- except for the provision of dynamic content, which is the most important aspect of the system. You need a way to insert dynamic content into a prewritten text message. If you use one of the text-based template engines available, it will complete all the hard work for you.

  • Print
  • Feedback

Resources
  • Velocity and WebMacro are the two most popular template engines:
  • Read more about MVC and what it can do for your program design:
  • Template-based Web application frameworks:
  • Object-relational tools:
  • JavaWorld Resources: