Results 1 to 2 of 2
  1. #1

    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.


  2. #2
    Join Date
    Apr 2002
    Denver, Colorado
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts