use CGI::Carp; use HTML::Template;
package Some::Module;
package CS205::Error;
use CGI;
use CGI::Carp qw( fatalsToBrowser );
BEGIN {
# Set the version for version checking
$VERSION = "0.1";
# Export the error subroutine
use Exporter;
@ISA = "Exporter";
@EXPORT = qw( error );
sub handleError {
my $message = shift;
my $q = new CGI;
error($q, $message);
}
CGI::Carp::set_message( \&handleError );
}
sub error {
my ($q, $message) = @_;
print $q->header("text/html"),
$q->start_html("Error"),
$q->h1("Error"),
$q->p("An error has occurred:"),
$q->p($q->em($message)),
$q->end_html;
exit;
}
1;
use CGI;
use CS205::Error;
my $q = new CGI;
unless ($q->param("action")) {
error($q, "No action specified");
}
print $q->header("text/html");
print "action = ", $q->param("action");
Last modified: <!--#echo var="LAST_MODIFIED"-->
<!--#command attribute="value" ... -->
<!--#echo var="var_name"-->
Dear user from host <!--#echo var="REMOTE_HOST"--> Last modified: <!--#echo var="LAST_MODIFIED"-->
<!--#include file="footer.html"--> <!--#include virtual="/~user/includes/footer.html"-->
<html>
<head><title>ACME Ltd. Newsletter</title></head>
<body>
<table>
<tr>
<td colspan="2">
<!--#include virtual="header.html"-->
<tr>
<td>
<!--#include virtual="navbar.html"-->
<td>
<!--#include virtual="main.html"-->
<tr>
<td colspan="2">
<!--#include virtual="footer.html"-->
</table>
</body>
</html>
This page has been accessed <!--#include virtual="/cgi/counter.cgi"--> times.
<html> <head><title>Current Time</title></head> <body> <h1>Current Time</h1> Welcome. The current time is <tmpl_var name="time">. </body> </html>
use HTML::Template; # open the template file my $template = new HTML::Template( filename => 'current-time.tmpl' ); # get local time my $time = localtime(); # fill in the template parameters $template->param( time => $time ); # print the content-type print "Content-Type: text/html\n\n"; # print the template print $template->output;
<tmpl_var name="paramName">
<tmpl_loop name="paramName">
...
</tmpl_loop>
<tmpl_if name="paramName">
...
</tmpl_if>
<tmpl_include name="file/path">
<html>
<head><title>Profile Viewer</title></head>
<body>
<h1>Profile Viewer</h1>
<hr>
<table border cellpadding="4" width="400">
<tmpl_loop name="profiles">
<tr>
<td><tmpl_var name="name">
<td><tmpl_var name="location">
<td><tmpl_var name="age">
</tr>
</tmpl_loop>
</table>
</body>
</html>
use HTML::Template;
# open the template file
my $template = new HTML::Template( filename => 'loops.tmpl' );
# set the table data
my @profiles = (
{ name => "Bob",
location => "Ottawa",
age => 33 },
{ name => "Alice",
location => "Toronto",
age => 25 },
{ name => "Mary",
location => "Montreal",
age => 21 },
);
# fill in the template parameters
$template->param( profiles => \@profiles );
# print the content-type
print "Content-Type: text/html\n\n";
# print the template
print $template->output;
<table> <tmpl_loop name="results"> <tr> <td><tmpl_var name="title"> <td><a href="<tmpl_var name="url">"><tmpl_var name="url"></a> </tmpl_loop> <tmpl_unless name="results"> <tr> <td colspan="2">No titles match your query </tmpl_unless> </table>
http://search.cpan.org/search?module=HTML::Template
HTML-Template-2.2
HTML/Template.pm
pod2html --infile Template.pm --outfile Template.html