Introduction to templating with Perl

Discussion in 'Perl' started by pradeep, Mar 26, 2008.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in

    Introduction



    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



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

    Code:
       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.
    HTML:
       <html>
       <head>
           <title><%=title%></title>
       </head>
       <body>
       <!-- heading -->
       <h2>Class <%=class%>, total students <%=tot_students%><h2>
       
       <table>
       <tr>
           <th>Name</th><th>Date of birth</th>
       </tr>
       <%loop student_details%>
       <tr>
           <td><%=_.NAME%></td>
           <td><%=_.DOB%></td>
       </tr>
       <%/loop%>
       </table>
       <br><br>
       <center>Time on server (<%=time%>)</center>
       
    And now, our business logic i.e. the Perl code.
    Code:
       #!/usr/bin/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);
           push(@student_details,\%tmp);
       }
       
       ## assign the variables to template engine
       $tpl->param('student_details%',\@student_details);
       $tpl->param('title','Templates in Perl');
       $tpl->param('class',$class);
       $tpl->param('tot_students',scalar(keys %students));
       $tpl->param('time',strftime('%T',localtime));
       
       ## output it to the browser
       print $cgi->header(),$tpl->output;
       

    Conclusion



    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!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice