Article

Embedding Perl Into Web Pages

Page: 1 2 3 4 5 6 7 8 9 10 11 Next

Templates

The philosophy of a template file is to make up a template describing the basic HTML layout that we wish to use. Within this file there are special indicators, which will be filled in dynamically from some script. There are a number of Perl modules available that can be used for this purpose, and like the question of 'Which editor should I use?', deciding which is best is often a matter of personal taste. Here we describe two basic modules to give a flavor for how they are used. For additional details and a list of further such modules, we ought to browse CPAN – a convenient way to do this is through a CPAN search engine such as:

  • http://search.cpan.org
  • http://www.perldoc.com
  • http://theoryx5.uwinnipeg.ca/mod_perl/cpan-search

'HTML::Template'

A basic template with this module has the following form:

<HTML>  
<HEAD><TITLE>Test Template</TITLE></HEAD>  
<BODY>      
 <TMPL_INCLUDE NAME="header.tmpl">    
  Please contact the person listed below: <BR>  
 <A HREF="mailto:<TMPL_VAR NAME=email>">  
 <TMPL_VAR NAME=person>  </A>    <TMPL_INCLUDE NAME="footer.tmpl">

Common headers and footers can be inserted with the <TMPL_INCLUDE NAME="file"> syntax, as indicated. If we call this template greeting1.tmpl, then we can generate a dynamic page using it as in the following script:

#!/usr/bin/perl  
# htmltemplate.pl  
use warnings;  
use strict;  
use HTML::Template;  
my $template = HTML::Template->new(filename => 'greeting1.tmpl');  
$template->param('email', 'bill@nowhere.com');  
$template->param('person', 'William');  
print "Content-Type: text/html\n\n";  
print $template->output;

The output method is then used to generate the filled-in template page.

'Template'

In the Template module (part of the Template-Toolkit package), the basic HTML template has variable placeholders indicated by the [% ... %] syntax, as in the following example:

[% INCLUDE header %]  
   Please contact the person listed below: <BR>  
   <a href="mailto:[% email %]">[% person %]</a>  
[% INCLUDE footer %]

Note the use of INCLUDE to include common headers and footers. If we call this template greeting2.html, then we can generate a dynamic page using this template as in the following script:

#!/usr/bin/perl  
# template.pl  
use warnings;  
use strict;  
use Template;  
$| = 1;  
print "Content-type: text/html\n\n";  
my $file = 'greeting2.html';  
my $vars = {  
   'email' => 'bill@nowhere.com',  
   'person' => 'William',  
};  
my $template = Template->new({  
   INCLUDE_PATH => '/home/me/templates:/home/me/lib',  
});  
$template->process($file, $vars) || die $template->error();

Note the use of INCLUDE_PATH when creating the Template object to specify which directories to search for the template files. The process method is the basic workhorse of the module – it takes the template specified by $file, and applies the variable substitutions in this file specified by $vars.

These modules, and others, support more than just simple variable substitutions. For example, in the Template module, simple loop constructions and conditionals can be incorporated. If in the script we set the following:

$vars = {  
   'people' => [ 'Tom', 'Dick', 'Larry', 'Mary' ],  
};

and in the template file:

[% FOREACH person = people %]  
Hello [% person %]  
[% END %]

the script will iterate over the members of the people array reference and print out the corresponding value (individual members can be accessed in the template as, for example, [% people.1 %], which will correspond to Dick in the above example). In addition to insertion, replacement, loops, and conditionals, the Template module has directives for dynamically invoking other template files, a macro capability, exception raising and handling, and some invocation of Perl code. For example, the processing of form data can be done within the Template module through the CGI.pm module simply by including the following in the template file:

[% USE CGI %]

The methods of the CGI.pm module can then be accessed through syntax such as:

[% CGI.header %]  
[% CGI.param('parameter') %]

These template modules can be used in a mod_perl-enabled server to speed up generation of the pages. One of the advantages of these approaches is that a relatively clean separation between content and display is obtained. Due to the relative simplicity of the template indicators, it is reasonable to have, for example, a web designer work on a template file and a programmer work on the data generation. However, for more complex situations, a greater degree of programming constructions may be required and these will be described in the next sections.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links