Results 1 to 2 of 2
Thread: Templating in Perl
Threaded View
-
08-07-2005, 12:51 AM #1Disabled
- Join Date
- Jan 2005
- Location
- USA
- Posts
- 1,379
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:
Code:use HTML::Template; $tmpl = new HTML::Template(filename => 'test.tmpl');
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:
Code:$tmpl->param(name => 'Brandon');
Finally, to output the template in its entirety, you use the output method, as if that were a shock.
Code:print $tmpl->output;
It is quite simple. To access a variable, you use something like:
Code:<TMPL_VAR NAME="name">
Code:<TMPL_VAR NAME="name" DEFAULT="anonymous">
There are also if statements, in this form:
Code:<TMPL_IF NAME="varname"> What to do here if varname is true <TMPL_ELSE> What to do if varname is false </TMPL_IF>
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 --------------------
Code:#!/usr/bin/perl # 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
Code:<html> <head> <title>Template</title> </head> <body> <h1>Template</h1> <TMPL_IF NAME="name"> <p>Hello <TMPL_VAR NAME="name" DEFAULT="anonymous" ESCAPE="HTML">.</p> <TMPL_ELSE> <form action="index.cgi" method="post"> Name: <input type="text" name="name"> <input type="submit" value="Submit"> </form> </TMPL_IF> </body> </html>
Brandon