21
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

Code from LPW 2012 Template::Toolkit Workshop

Embed Size (px)

DESCRIPTION

Source of the templates and the Perl files used to run them. Converted to a PDF.

Citation preview

Page 1: 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

Page 2: Code from LPW 2012 Template::Toolkit Workshop

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,

Page 3: Code from LPW 2012 Template::Toolkit Workshop

}) || 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";

Page 4: Code from LPW 2012 Template::Toolkit Workshop

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";

Page 5: Code from LPW 2012 Template::Toolkit Workshop

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

Page 6: Code from LPW 2012 Template::Toolkit Workshop

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 %]

Page 7: Code from LPW 2012 Template::Toolkit Workshop

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.

Page 8: Code from LPW 2012 Template::Toolkit Workshop

[% 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";

Page 9: Code from LPW 2012 Template::Toolkit Workshop

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

Page 10: Code from LPW 2012 Template::Toolkit Workshop

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";

Page 11: Code from LPW 2012 Template::Toolkit Workshop

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' },

]

}

Page 12: Code from LPW 2012 Template::Toolkit Workshop

) || 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' },

Page 13: Code from LPW 2012 Template::Toolkit Workshop

]

}

) || 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' },

Page 14: Code from LPW 2012 Template::Toolkit Workshop

{ 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 },

Page 15: Code from LPW 2012 Template::Toolkit Workshop

{ 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',

{

Page 16: Code from LPW 2012 Template::Toolkit Workshop

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>

Page 17: Code from LPW 2012 Template::Toolkit Workshop

<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 %] -->

Page 18: Code from LPW 2012 Template::Toolkit Workshop

[%# 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>

Page 19: Code from LPW 2012 Template::Toolkit Workshop

[% 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>

Page 20: Code from LPW 2012 Template::Toolkit Workshop

[% 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>&pound;[% 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>&pound;[% money(r.cost)

%]</td></tr>

[% END %]

</table>

<!-- End [% component.name %] -->

Page 21: Code from LPW 2012 Template::Toolkit Workshop