##
Hello
##
##
# secret.pl
my $template = HTML::Template->new(filename => 'secret2.tmpl');
$template->param(
ILLUMINATI => is_member($id), # assume sub returns 0 or 1
SECRET_MESSAGE => 'There is no Perl Illuminati',
);
print $template->output;
##
##
Move along, nothing to see . . .
# secret.pl
my $message = 'Yes there is' if is_member($id);
my $template = HTML::Template->new(filename => 'secret2.tmpl');
$template->param(SECRET => $message);
print $template->output;
##
##
Name:
GPA:
# students.pl
my $template = HTML::Template->new(filename => 'students.tmpl');
$template->param(
STUDENT => [
{ NAME => 'Bluto Blutarsky', GPA => '0.0' },
{ NAME => 'Tracey Flick' , GPA => '4.0' },
]
);
print $template->output;
##
##
Song Listing
My Songs
# songs.cgi
use DBI;
use CGI;
use HTML::Template;
use strict;
my $DBH = DBI->connect(
qw(DBI:vendor:database:host user pass),
{ RaiseError => 1}
);
my $CGI = CGI->new();
# grab the stuff from the database
my $sth = $DBH->prepare('
select title, artist, album, year
from songs
');
$sth->execute();
# prepare a data structure for HTML::Template
my $rows;
push @{$rows}, $_ while $_ = $sth->fetchrow_hashref();
# instantiate the template and substitute the values
my $template = HTML::Template->new(filename => 'songs.tmpl');
$template->param(ROWS => $rows);
print $CGI->header();
print $template->output();
$DBH->disconnect();
##
##
push @{$rows}, $_ while $_ = $sth->fetchrow_hashref();
##
##
{
'artist' => 'Van Halen',
'title' => 'Spanish Fly',
'album' => 'Van Halen II',
'year' => '1979',
};
##
##
# we don't need no stinkin' column names
my $rows = $DBH->selectall_arrayref('select * from songs');
# don't croak on template names that don't exist
my $template = HTML::Template->new(
filename => 'mp3.tmpl',
die_on_bad_params => 0,
);
$template->param(ROWS => $rows);
##
##
my $CGI = CGI->new();
my $template = HTML::Template->new(
filename => 'foo.tmpl',
associate => $CGI,
);
##
##
the first is usually a header
odd rows are red
even rows are blue
you have no chance to survive so choose
# pill.cgi
my $template = HTML::Template->new(
filename => 'pill.tmpl',
loop_context_vars => 1,
);
# etc.
##
##
my $template = HTML::Template->new(scalarref => \$scalar);
##
##
#!/usr/bin/perl -Tw
use DBI;
use CGI;
use HTML::Template;
use strict;
my $DBH = DBI->connect(
qw(DBI:mysql:mp3:host user pass),
{ RaiseError => 1 },
);
my $CGI = CGI->new();
my @COLS = (qw(title artist album));
# verify the sort param - never trust user input
my %sort_lookup = map {$_ => $_} @COLS;
my $sort = $sort_lookup{$CGI->param('sort')||''} || 'title';
my $data = $DBH->selectall_arrayref("
select @{[join(',', @COLS)]}
from songs
order by ?
", undef, ($sort));
# prepare the DS for the headers
my $headers = [
map {{
URL => $CGI->script_name . "?sort=$_",
LINK => ucfirst($_),
}} @COLS
];
# prepare the DS for the rows
my $i;
my $rows = [
map {
my $row = $_;
(++$i % 2)
? { ODD => [ map { {VALUE => $_} } @{$row} ] }
: { EVEN => [ map { {VALUE => $_} } @{$row} ] }
} @{$data}
];
# remove excess blood from ears after that last expression
# read the template as a scalar from DATA
my $html = do { local $/; };
# prepare the template and substitute the values
my $template = HTML::Template->new(
scalarref => \$html,
loop_context_vars => 1,
);
$template->param(
HEADERS => $headers,
ROWS => $rows,
SORT => $sort,
);
# print the goods
print $CGI->header();
print $template->output();
$DBH->disconnect();
__DATA__
Songs sorted by
Songs sorted by