Introduction to templating with Perl

pradeep's Avatar author of Introduction to templating with Perl
This is an article on Introduction to templating with Perl in Perl.


Templating, you can say is a way to separate the business logic from the presentation logic. Most of the world's we application developers use either embeded scripting languages like PHP, JSP, ASP etc. while other use print statments in the scripting languages to output their HTML to the world. Templating can be overlooked for small pages and websites, but as your business logic becomes complex and you starting getting more hits, maintanance and upgradation becomes tough.
A templating system allows the designer to write theri own HTML with special commands/placeholders in it for the programmer to integrate the business logic into it, thereby making each others lives easier.

Why Templates?

There are numerous benefits of using templates, let's look at a few of them.
  • Consistent UI - You make one navigation bar, and use it on every page that needs it.
  • Reusablity - You can create common modules like headers, footers, navigation bars and use it on new pages.
  • Easier to change - Doesn't matter if you change you business logic, the UI doesn't need to modified.
  • Caching - Many template engines can compile & cache your templates so that they load & execute faster on the next run.

Template Engines

There are quite a few notable template engines for Perl, namely HTML::Mason, Template::Toolkit, HTML::Template; Smarty is considered one of the best template engines for PHP.
In our example we will use HTML::Template::Compiled, it's the same as HTML::Template except for the fact that it compiles template files to Perl code for faster execution, plus a few addition features which you can checkout here.


HTML::Template::Compiled can run in a compatibility mode to be compatible with HTML::Template.

Code: Perl
use HTML::Template::Compiled speed => 1;
   # or for compatibility with HTML::Template
   use HTML::Template::Compiled compatible => 1;
   # or
   use HTML::Template::Compiled::Classic
Let's say we have a page where we want to show the names of all the student os a class and their date of birth. Here's out template for that purpose.
Code: HTML
   <!-- heading -->
   <h2>Class <%=class%>, total students <%=tot_students%><h2>
       <th>Name</th><th>Date of birth</th>
   <%loop student_details%>
   <center>Time on server (<%=time%>)</center>
And now, our business logic i.e. the Perl code.
Code: Perl
   use HTML::Template::Compiled speed => 1;
   use CGI;
   use POSIX qw/strftime/;
   my $tpl = HTML::Template::Compiled->new(filename => 'test.tmpl.htm');
   my $cgi = new CGI;
   my %students = ('Shabbir','24th June,1982','Pradeep','12th August,1985','Deepak','30th December, 1981');
   ## you can fetch your data from a database instead
   my @student_details;
   my $class = 'XII';
   while(my($name,$dob) = each(%students))
       my %tmp = ('NAME',$name,'DOB',$dob);
   ## assign the variables to template engine
   $tpl->param('title','Templates in Perl');
   $tpl->param('tot_students',scalar(keys %students));
   ## output it to the browser
   print $cgi->header(),$tpl->output;


In the beginning you might feel this a useless thing to do, but as you grow and as you write better and complex websites and web apps, you'll understand the benefits of templating system better. Happy coding!