Code from LPW 2012 Template::Toolkit Workshop
description
Transcript of Code from LPW 2012 Template::Toolkit Workshop
Code From LPW 2012 Template::Toolkit Workshop
Contents Code From LPW 2012 Template::Toolkit Workshop ........................................................................... 1
Part 1 ............................................................................................................................................ 2
s1.cgi ......................................................................................................................................... 2
s2.cgi ......................................................................................................................................... 2
s3.cgi ......................................................................................................................................... 3
s4.cgi ......................................................................................................................................... 4
Angry_letter.tt2 ......................................................................................................................... 5
s1.tt2 ......................................................................................................................................... 6
s2.tt2 ......................................................................................................................................... 6
s3.tt2 ......................................................................................................................................... 7
s4.tt2 ......................................................................................................................................... 7
Part2 ............................................................................................................................................. 8
s1.cgi ......................................................................................................................................... 8
s2.cgi ......................................................................................................................................... 9
s3a.cgi ....................................................................................................................................... 9
s3b.cgi ..................................................................................................................................... 11
s4.cgi ....................................................................................................................................... 12
s5.cgi ....................................................................................................................................... 13
s6a.cgi ..................................................................................................................................... 14
s6b.cgi ..................................................................................................................................... 15
wrapper.tt2 ............................................................................................................................. 16
s1.tt2 ....................................................................................................................................... 16
s2.tt2 ....................................................................................................................................... 17
s3.tt2 ....................................................................................................................................... 17
s4.tt2 ....................................................................................................................................... 18
s5.tt2 ....................................................................................................................................... 18
s6a.tt2 ..................................................................................................................................... 19
s6b.tt2 ..................................................................................................................................... 20
Part 1
s1.cgi
#! /usr/bin/perl
use strict;
use Template;
my $tt = Template->new({
INCLUDE_PATH => '../../htdocs/tt2/part1',
INTERPOLATE => 1,
}) || die "$Template::ERROR\n";
my $vars = {
name => 'Count Edward van Halen',
debt => '3 riffs and a solo',
deadline => 'the next chorus',
};
$tt->process('s1.tt2', $vars)
|| die $tt->error(), "\n";
s2.cgi
#! /usr/bin/perl
use strict;
use Template;
my $tt = Template->new({
INCLUDE_PATH => '../../htdocs/tt2/part1',
INTERPOLATE => 1,
}) || die "$Template::ERROR\n";
my $vars = { debtors => [
{
name => 'Count Edward van Halen',
debt => '3 riffs and a solo',
deadline => 'the next chorus',
},
{
name => 'Baron Eric Clapton',
debt => '1 badge',
deadline => 'tomorrow',
}
]
};
$tt->process('s2.tt2', $vars)
|| die $tt->error(), "\n";
s3.cgi
#! /usr/bin/perl
use strict;
use Template;
my $tt = Template->new({
INCLUDE_PATH => '../../htdocs/tt2/part1',
INTERPOLATE => 1,
}) || die "$Template::ERROR\n";
my $vars = { debtors => [
{
name => 'Count Edward van Halen',
debt => '3 riffs and a solo',
deadline => 'the next chorus',
},
{
name => 'Baron Eric Clapton',
debt => '1 badge',
deadline => 'tomorrow',
final => 1
}
]
};
$tt->process('s3.tt2', $vars)
|| die $tt->error(), "\n";
s4.cgi
#! /usr/bin/perl
use strict;
use Template;
my $tt = Template->new({
INCLUDE_PATH => '../../htdocs/tt2/part1',
INTERPOLATE => 1,
}) || die "$Template::ERROR\n";
my $vars = { debtors => [
{
name => 'Count Edward van Halen',
debt => '3 riffs and a solo',
deadline => 'the next chorus',
},
{
name => 'Baron Eric Clapton',
debt => '1 badge',
deadline => 'tomorrow',
final => 1,
angry => 1,
}
]
};
$tt->process('s4.tt2', $vars)
|| die $tt->error(), "\n";
Angry_letter.tt2
[%# Angry letter -%]
It has come to our attention that your account is in
arrears to the sum of [% d.debt %].
Please settle your account before [% d.deadline %] or we
will be forced to revoke your Licence to Thrill.
Grateful_letter.tt2
Thank you for your recent payment of [% d.debt %].
Your account is now clear and you may continue to thrill.
s1.tt2
[%# Basic letter -%]
Dear [% name %],
It has come to our attention that your account is in
arrears to the sum of [% debt %].
Please settle your account before [% deadline %] or we
will be forced to revoke your Licence to Thrill.
The Management.
s2.tt2
[%# Multiple letters -%]
[% FOREACH d = debtors %]
Dear [% d.name %],
It has come to our attention that your account is in
arrears to the sum of [% d.debt %].
Please settle your account before [% d.deadline %] or we
will be forced to revoke your Licence to Thrill.
The Management.
[% END %]
s3.tt2
[%# Multiple letters with logic %]
[% FOREACH d = debtors %]
Dear [% d.name %],
It has come to our attention that your account is in
arrears to the sum of [% d.debt %].
Please settle your account before [% d.deadline %] or we
will be forced to revoke your Licence to Thrill.
[% IF d.final %]This is your last chance.[% END %]
The Management.
[% END %]
s4.tt2
[%# Include other templates -%]
[% FOREACH d IN debtors %]
Dear [% d.name %],
[% IF d.angry -%]
[% INCLUDE angry_letter.tt2 %]
[% ELSE -%]
[% INCLUDE grateful_letter.tt2 %]
[% END -%]
The Management.
[% END %]
Part2
s1.cgi
#! /usr/bin/perl
# Header and footer will be the same on all pages %]
use strict;
use warnings;
use Template;
my $tt = Template->new(
{
INCLUDE_PATH => '/home/duncan/tt/htdocs/tt2/part2',
INTERPOLATE => 1,
}
) || die "$Template::ERROR\n";
$tt->process(
's1.tt2',
{
rows => [
{ something => 'apples', something_else => '1 kg' },
{ something => 'pears', something_else => '2 kg' }
]
}
) || die $tt->error() . "\n";
s2.cgi
#! /usr/bin/perl
use strict;
use warnings;
use Template;
# Using a wrapper.
my $tt = Template->new(
{
INCLUDE_PATH => '/home/duncan/tt/htdocs/tt2/part2',
INTERPOLATE => 1,
WRAPPER => 'wrapper.tt2'
}
) || die "$Template::ERROR\n";
$tt->process(
's2.tt2',
{
rows => [
{ something => 'apples', something_else => '1 kg' },
{ something => 'pears', something_else => '2 kg' }
]
}
) || die $tt->error() . "\n";
s3a.cgi
#! /usr/bin/perl
use strict;
use warnings;
use Template;
# Using a filter to encode entities and control case. Code injection attack.
my $tt = Template->new(
{
INCLUDE_PATH => '/home/duncan/tt/htdocs/tt2/part2',
INTERPOLATE => 1,
WRAPPER => 'wrapper.tt2'
}
) || die "$Template::ERROR\n";
$tt->process(
's2.tt2',
{
rows => [
{ something => 'apples', something_else => '1 kg' },
{ something => 'pears', something_else => '2 kg' },
{ something => 'turnips > pears > grapes', something_else => '2 & 3 kg' },
{ something => '<script type="text/javascript">alert("Hi");</script>', something_else => '2 & 3
kg' },
]
}
) || die $tt->error() . "\n";
s3b.cgi
#! /usr/bin/perl
# Using a filter to encode entities and control case. Entities encoded to prevent code injection
attack.
use strict;
use warnings;
use Template;
my $tt = Template->new(
{
INCLUDE_PATH => '/home/duncan/tt/htdocs/tt2/part2',
INTERPOLATE => 1,
WRAPPER => 'wrapper.tt2'
}
) || die "$Template::ERROR\n";
$tt->process(
's3.tt2',
{
rows => [
{ something => 'apples', something_else => '1 kg' },
{ something => 'pears', something_else => '2 kg' },
{ something => 'turnips > pears > grapes', something_else => '2 & 3 kg' },
{ something => '<script type="text/javascript">alert("Hi");</script>', something_else => '2 & 3
kg' },
]
}
) || die $tt->error() . "\n";
s4.cgi
#! /usr/bin/perl
# Using a vmethod to add row numbers
use strict;
use warnings;
use Template;
my $tt = Template->new(
{
INCLUDE_PATH => '/home/duncan/tt/htdocs/tt2/part2',
INTERPOLATE => 1,
WRAPPER => 'wrapper.tt2'
}
) || die "$Template::ERROR\n";
$tt->process(
's4.tt2',
{
rows => [
{ something => 'apples', something_else => '1 kg' },
{ something => 'pears', something_else => '2 kg' },
{ something => 'turnips > pears > grapes', something_else => '2 & 3 kg' },
{ something => '<>peas', something_else => '2 & 3 kg' },
{ something => 'apricots', something_else => '2 & 3 kg' },
]
}
) || die $tt->error() . "\n";
s5.cgi
#! /usr/bin/perl
# Using a vmethod to do a simple sort on a select list.
use strict;
use warnings;
use Template;
my $tt = Template->new(
{
INCLUDE_PATH => '/home/duncan/tt/htdocs/tt2/part2',
INTERPOLATE => 1,
WRAPPER => 'wrapper.tt2'
}
) || die "$Template::ERROR\n";
$tt->process(
's5.tt2',
{
rows => [
{ something => 'apples', something_else => '1 kg' },
{ something => 'pears', something_else => '2 kg' },
{ something => 'turnips > pears > grapes', something_else => '2 & 3 kg' },
{ something => '<>peas', something_else => '2 & 3 kg' },
{ something => 'apricots', something_else => '2 & 3 kg' },
],
suppliers => [ 'Fred', 'Alan', 'Joe', 'Bert' ]
}
) || die $tt->error() . "\n";
s6a.cgi
#! /usr/bin/perl
# Using a plugin to format currency
use strict;
use warnings;
use Template;
my $tt = Template->new(
{
INCLUDE_PATH => '/home/duncan/tt/htdocs/tt2/part2',
INTERPOLATE => 1,
WRAPPER => 'wrapper.tt2'
}
) || die "$Template::ERROR\n";
$tt->process(
's6a.tt2',
{
rows => [
{ something => 'apples', something_else => '1 kg', cost => 5.67 },
{ something => 'pears', something_else => '2 kg', cost => 3.333333333333333333 },
{ something => 'turnips > pears > grapes', something_else => '2 & 3 kg', cost => 4.123 },
{ something => '<>peas', something_else => '2 & 3 kg', cost => 2 },
{ something => 'apricots', something_else => '2 & 3 kg', cost => 3 },
],
suppliers => [ 'Fred', 'Alan', 'Joe', 'Bert' ]
}
) || die $tt->error() . "\n";
s6b.cgi
#! /usr/bin/perl
# Using a vmethod to do a simple sort on a select list.
use strict;
use warnings;
use Template;
my $tt = Template->new(
{
INCLUDE_PATH => '/home/duncan/tt/htdocs/tt2/part2',
INTERPOLATE => 1,
WRAPPER => 'wrapper.tt2'
}
) || die "$Template::ERROR\n";
$tt->process(
's6b.tt2',
{
rows => [
{ something => 'apples', something_else => '1 kg', cost => 5.67 },
{ something => 'pears', something_else => '2 kg', cost => 3.333333333333333333 },
{ something => 'turnips > pears > grapes', something_else => '2 & 3 kg', cost => 4.123 },
{ something => '<>peas', something_else => '2 & 3 kg', cost => 2 },
{ something => 'apricots', something_else => '2 & 3 kg', cost => 3 },
],
suppliers => [ 'Fred', 'Alan', 'Joe', 'Bert' ]
}
) || die $tt->error() . "\n";
wrapper.tt2 Content-Type: text/html; charset=ISO-8859-1
<html>
<head>
</head>
<body>
<h1>Introduction to TT</h2>
[% content %]
</body>
</html>
s1.tt2 Content-Type: text/html; charset=ISO-8859-1
<html>
<head>
</head>
<body>
<h1>Introduction to TT</h2>
[%# Header and footer will be the same on all pages %]
<table>
<tr><th>Something</th><th>Something Else</th></tr>
[% FOREACH r IN rows %]
<tr><td>[% r.something %]</td><td>[% r.something_else %]</td></tr>
[% END %]
</table>
</body>
</html>
s2.tt2 <!-- [% component.name %] -->
[%# Header and footer have been moved to a wrapper %]
<table>
<tr><th>Something</th><th>Something Else</th></tr>
[% FOREACH r IN rows %]
<tr><td>[% r.something %]</td><td>[% r.something_else %]</td></tr>
[% END %]
</table>
<!-- End [% component.name %] -->
s3.tt2 <!-- [% component.name %] -->
[%# Using a filter to encode entities and control case. %]
<table>
<tr><th>Something</th><th>Something Else</th></tr>
[% FOREACH r IN rows %]
<tr><td>[% r.something FILTER html_entity %]</td><td>[%
r.something_else FILTER upper FILTER html_entity %]</td></tr>
[% END %]
</table>
<!-- End [% component.name %] -->
s4.tt2 <!-- [% component.name %] -->
[%# Using a vmethod to add row number %]
<table>
<tr><th>No</th><th>Something</th><th>Something Else</th></tr>
[% FOREACH r IN rows %]
<tr><td>([% loop.index %])</td><td>[% r.something FILTER html_entity
%]</td><td>[% r.something_else FILTER html_entity FILTER upper
%]</td></tr>
[% END %]
</table>
<!-- End [% component.name %] -->
s5.tt2
[%# Using a vmethod to do a simple sort on a select list %]
<! [% component.name %] -->
<select>
[% FOREACH s IN suppliers.sort %]
<option value="s"/>[% s %]
[% END %]
</select>
<table>
<tr><th>No</th><th>Something</th><th>Something Else</th></tr>
[% FOREACH r IN rows %]
<tr><td> ([% loop.index %])</td><td>[% r.something FILTER html_entity %]</td><td>[%
r.something_else FILTER html_entity FILTER upper %]</td></tr>
[% END %]
</table>
<!-- End [% component.name %] -->
s6a.tt2
[%# Using a plugin to format currency %]
<! [% component.name %] -->
<select>
[% FOREACH s IN suppliers.sort %]
<option value="s"/>[% s %]
[% END %]
</select>
<table>
<tr><th>No</th><th>Something</th><th>Something Else</th><th>Cost</ht></tr>
[% FOREACH r IN rows %]
<tr><td> ([% loop.index %])</td><td>[% r.something FILTER html_entity %]</td><td>[%
r.something_else FILTER html_entity FILTER upper %]</td><td>£[% r.cost %]</td></tr>
[% END %]
</table>
<!-- End [% component.name %] -->
s6b.tt2
[%# Using a plugin to format currency %]
<! [% component.name %] -->
[% USE money=format('%.2f') -%]
<select>
[% FOREACH s IN suppliers.sort %]
<option value="s"/>[% s %]
[% END %]
</select>
<table>
<tr><th>No</th><th>Something</th><th>Something Else</th><th>Cost</ht></tr>
[% FOREACH r IN rows %]
<tr><td> ([% loop.index %])</td><td>[% r.something FILTER html_entity %]</td><td>[%
r.something_else FILTER html_entity FILTER upper %]</td><td>£[% money(r.cost)
%]</td></tr>
[% END %]
</table>
<!-- End [% component.name %] -->