hosted by liquidweb

Go Back   Web Hosting Talk : Web Hosting Main Forums : Web Hosting Talk Tutorials : Programming Tutorials : Templating in Perl

Forum Jump

Templating in Perl

Reply Post New Thread In Programming Tutorials Subscription
Send news tip View All Posts Thread Tools Search this Thread Display Modes
Web Hosting Master
Join Date: Jan 2005
Location: UK
Posts: 1,368

Templating in Perl

In this tutorial I'm going to show a simple way to use templating in Perl. First, let me explain what templating is and why it is useful.

Templating allows you to have a separate template file with any HTML in it which will be on the web page. In that template, you refer to certain variables assigned to it through a related Perl script. You access these variables through a special syntax defined by that templating module.

Why would you want to do this? Often, programmers and designers have to work together on a single project. Generally, designers don't want to mess with code and programmers don't want to mess with HTML. Templating makes both sides happy. With HTML::Template, you use HTML-like tags for accessing variables and other such constructs.

First you have to create the templating object. The code looks something like this:

use HTML::Template;

$tmpl = new HTML::Template(filename => 'test.tmpl');
For some odd reason the standard with this module at least seems to be to use the .tmpl extension, instead of the more common .tpl. But, I'm not one to break standards, especially when CPAN itself uses them.

Anyway, this code is easy enough to understand. You instantiate a new object of HTML::Template, opening the template called test.tmpl. See the documentation for other options you can pass to the constructor.

Next, you assign variables for the template to use. You can do this through the param method, which has a syntax like this:

$tmpl->param(name => 'Brandon');
You can also assign arrays for use in loops, but you can read about that in the documentation.

Finally, to output the template in its entirety, you use the output method, as if that were a shock.

print $tmpl->output;
Now onto the syntax of the template itself.

It is quite simple. To access a variable, you use something like:

<TMPL_VAR NAME="name">
If you want a default to be used if the variable is empty, you can use:

<TMPL_VAR NAME="name" DEFAULT="anonymous">
If you want to escape the variable so < turns to &lt;, > to &gt;, " to &amp;quot;, ' to &amp;#039;, etc, then use ESCAPE="HTML"

There are also if statements, in this form:

<TMPL_IF NAME="varname">
What to do here if varname is true
What to do if varname is false
There is even a TMPL_UNLESS, having the same syntax.

Yes, there are loops, but they will not be covered here.

Finally, here is a program implementing all of this. It has a simple form, asking you to enter your name, and displays that name on the next page.

-------------------- index.cgi --------------------


# A script to test HTML::Template

use strict;
use warnings;
use diagnostics;
use CGI::Carp qw/fatalsToBrowser/;    # Useful errors, please!
use HTML::Template;
use CGI qw/:standard/;                # For form input

print header;                         # Same old header

my $tmpl =
  new HTML::Template( filename => 'templates/index.tmpl' )
  ;                                   # Open up templates/index.tmpl

if (param) {                          # Was the form submitted?
    $tmpl->param( name => param('name') )
      ;    # Assign the form input for name to the name variable

print $tmpl->output;    # Print out the template
-------------------- index.tmpl --------------------




<TMPL_IF NAME="name">
<p>Hello <TMPL_VAR NAME="name" DEFAULT="anonymous" ESCAPE="HTML">.</p>
<form action="index.cgi" method="post">
Name: <input type="text" name="name">
<input type="submit" value="Submit">

Hope that may prove useful.


Sponsored Links
Web Hosting Guru
Join Date: Apr 2002
Location: Denver, Colorado
Posts: 269
you didn't show anything on TMPL_LOOPs or TMPL_UNLESS, or TMPL_INCLUDE, which I think are some of the most useful features of H::T. However, I'm glad you didn't try to write your own. Good tutorial on the simple stuff, check out Template Toolkit as well!

I <3 perl
Get it CodedRight&copy;


Related posts from
Title Type Date Posted
Media Temple Expands Managed WordPress Hosting Line for Web Designers and Developers Web Hosting News 2015-02-25 16:01:36
The Story of PHP and Joomla!: 2014 Web Hosting News 2014-04-01 10:33:59
Linux Malware Operation Windigo Infects 25,000 Web Servers Web Hosting News 2014-03-19 11:44:53
Media Temple Launches Premium WordPress Hosting for Designers, Web Developers Web Hosting News 2014-03-04 14:24:41
cPanel & WHM Software 11.36 Reaches Stable Tier Status Web Hosting News 2013-04-09 11:44:33

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Log in with your username and password

Forgot Password?
WHT Host Brief Email:

We respect your privacy. We will never sell, rent, or give away your address to any outside party, ever.

Web Hosting News:
WHT Membership
WHT Membership



Welcome to

Create your username to jump into the discussion! is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.

(4 digit year)

Already a member?