A template engine is a software development
tool that allows you to create and render
documents that format and present your data
.
A template engine is included as part of
a template system or application
framework
Content from a database and "presentation
specifications" (in a web template), are
combined (through the template engine) to
produce web documents.
 Examples are:
Engine Language
Apache Velocity Java, C#
ASP.net C#, VB.net
Casper Java, Javascript
String Template Java, Python
Cheetah Template Python
 Template engines generally include features common in
higher level programming with emphasis on processing
plain text
 Features include:
 Variables and functions
 Text replacement
 File inclusion
 Conditional evaluation and loops
Velocity Template Engine
It permits the templates to reference methods defined in Java
code using Velocity template language(VTL) statements.
VTL uses references to embed dynamic content in a text
product
References are of three types variables, properties and Methods
VTL REFERENCES
Variable ${aVariable}, $aVariable
Properties ${anIdentifier.aIdentifier}
Methods ${aIdentifier.aMethodBody(${aIdentifier})}
 References begin with $ and are used to get something
 Directives begin with # and are used to do something
 #set is used to assign a value to a variable
 The variable, $aVariable can the be used in the template
to output the value(i.e “aValue”)
#set($ aVariable = “aValue”)
Character
directive
Variable
Value
Lets Work!!
 Download velocity engine (Velocity-1.7 jar) from
http://velocity.apache.org/download.cgi
 Include the Velocity-1.7 jar jar in the lib folder of your
project folder(you may also have to include commons-
collections.jar, commons-lang and mail.jar).
 Prepare your .vsl template message
 Write your java code
Template Message
#*
Test E-mail Message
@zed
*#
Dear Mr $recepientName,
The management of $companyName hereby invite you for an
interview in $interviewVenue at $timeOfInterview .
Yours Sincerely,
$sendersName
Sample Code
public void sendMail() {
Template template = null;
template = ve.getTemplate("/myMessage.vsl");
Map<String, String> mymail = new HashMap<String, String>();
mymail.put("recepientName", infoDetail.getRecepientname());
mymail.put("companyName", infoDetail.getCompanyName());
mymail.put("interviewVenue", infoDetail.getInterviewVenue());
mymail.put("timeOfInterview", infoDetail.getTimeOfInterview());
mymail.put("sendersName", infoDetail.getSenderName());
VelocityContext context = new VelocityContext(mymail);
StringWriter writer = writer = new StringWriter();
if (template != null) {
template.merge(context, writer);
}
message = writer.toString();
}
Output
Dear Mr James,
The management of Seamfix hereby invite you for an
interview in Lagos at 8:00pm
Yours Sincerely,
Zebrudaya
QUESTIONS??
Using velocity Templates(An overview)

Using velocity Templates(An overview)

  • 2.
    A template engineis a software development tool that allows you to create and render documents that format and present your data . A template engine is included as part of a template system or application framework Content from a database and "presentation specifications" (in a web template), are combined (through the template engine) to produce web documents.
  • 3.
     Examples are: EngineLanguage Apache Velocity Java, C# ASP.net C#, VB.net Casper Java, Javascript String Template Java, Python Cheetah Template Python
  • 4.
     Template enginesgenerally include features common in higher level programming with emphasis on processing plain text  Features include:  Variables and functions  Text replacement  File inclusion  Conditional evaluation and loops
  • 5.
    Velocity Template Engine Itpermits the templates to reference methods defined in Java code using Velocity template language(VTL) statements. VTL uses references to embed dynamic content in a text product References are of three types variables, properties and Methods VTL REFERENCES Variable ${aVariable}, $aVariable Properties ${anIdentifier.aIdentifier} Methods ${aIdentifier.aMethodBody(${aIdentifier})}
  • 6.
     References beginwith $ and are used to get something  Directives begin with # and are used to do something  #set is used to assign a value to a variable  The variable, $aVariable can the be used in the template to output the value(i.e “aValue”) #set($ aVariable = “aValue”) Character directive Variable Value
  • 7.
    Lets Work!!  Downloadvelocity engine (Velocity-1.7 jar) from http://velocity.apache.org/download.cgi  Include the Velocity-1.7 jar jar in the lib folder of your project folder(you may also have to include commons- collections.jar, commons-lang and mail.jar).  Prepare your .vsl template message  Write your java code
  • 8.
    Template Message #* Test E-mailMessage @zed *# Dear Mr $recepientName, The management of $companyName hereby invite you for an interview in $interviewVenue at $timeOfInterview . Yours Sincerely, $sendersName
  • 9.
    Sample Code public voidsendMail() { Template template = null; template = ve.getTemplate("/myMessage.vsl"); Map<String, String> mymail = new HashMap<String, String>(); mymail.put("recepientName", infoDetail.getRecepientname()); mymail.put("companyName", infoDetail.getCompanyName()); mymail.put("interviewVenue", infoDetail.getInterviewVenue()); mymail.put("timeOfInterview", infoDetail.getTimeOfInterview()); mymail.put("sendersName", infoDetail.getSenderName()); VelocityContext context = new VelocityContext(mymail); StringWriter writer = writer = new StringWriter(); if (template != null) { template.merge(context, writer); } message = writer.toString(); }
  • 10.
    Output Dear Mr James, Themanagement of Seamfix hereby invite you for an interview in Lagos at 8:00pm Yours Sincerely, Zebrudaya
  • 11.