Code from LPW 2012 Template::Toolkit Workshop

755 views

Published on

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

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
755
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Code from LPW 2012 Template::Toolkit Workshop

  1. 1. Code From LPW 2012 Template::Toolkit WorkshopContentsCode 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
  2. 2. Part 1s1.cgi#! /usr/bin/perluse strict;use Template;my $tt = Template->new({ INCLUDE_PATH => ../../htdocs/tt2/part1, INTERPOLATE => 1,}) || die "$Template::ERRORn";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/perluse strict;use Template;my $tt = Template->new({ INCLUDE_PATH => ../../htdocs/tt2/part1, INTERPOLATE => 1,
  3. 3. }) || die "$Template::ERRORn";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/perluse strict;use Template;my $tt = Template->new({ INCLUDE_PATH => ../../htdocs/tt2/part1, INTERPOLATE => 1,}) || die "$Template::ERRORn";
  4. 4. 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/perluse strict;use Template;my $tt = Template->new({ INCLUDE_PATH => ../../htdocs/tt2/part1, INTERPOLATE => 1,}) || die "$Template::ERRORn";
  5. 5. 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 inarrears to the sum of [% d.debt %].Please settle your account before [% d.deadline %] or wewill be forced to revoke your Licence to Thrill.Grateful_letter.tt2
  6. 6. 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 inarrears to the sum of [% debt %].Please settle your account before [% deadline %] or wewill 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 inarrears to the sum of [% d.debt %].Please settle your account before [% d.deadline %] or wewill be forced to revoke your Licence to Thrill.The Management.[% END %]
  7. 7. s3.tt2[%# Multiple letters with logic %][% FOREACH d = debtors %]Dear [% d.name %],It has come to our attention that your account is inarrears to the sum of [% d.debt %].Please settle your account before [% d.deadline %] or wewill 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.
  8. 8. [% END %]Part2s1.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::ERRORn";$tt->process( s1.tt2, { rows => [ { something => apples, something_else => 1 kg }, { something => pears, something_else => 2 kg } ] }) || die $tt->error() . "n";
  9. 9. s2.cgi#! /usr/bin/perluse 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::ERRORn";$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
  10. 10. 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::ERRORn";$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 & 3kg }, ] }) || die $tt->error() . "n";
  11. 11. s3b.cgi#! /usr/bin/perl# Using a filter to encode entities and control case. Entities encoded to prevent code injectionattack.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::ERRORn";$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 & 3kg }, ] }
  12. 12. ) || die $tt->error() . "n";s4.cgi#! /usr/bin/perl# Using a vmethod to add row numbersuse strict;use warnings;use Template;my $tt = Template->new( { INCLUDE_PATH => /home/duncan/tt/htdocs/tt2/part2, INTERPOLATE => 1, WRAPPER => wrapper.tt2 }) || die "$Template::ERRORn";$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 },
  13. 13. ] }) || 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::ERRORn";$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 },
  14. 14. { 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 currencyuse strict;use warnings;use Template;my $tt = Template->new( { INCLUDE_PATH => /home/duncan/tt/htdocs/tt2/part2, INTERPOLATE => 1, WRAPPER => wrapper.tt2 }) || die "$Template::ERRORn";$tt->process( s6a.tt2, { rows => [ { something => apples, something_else => 1 kg, cost => 5.67 },
  15. 15. { 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::ERRORn";$tt->process( s6b.tt2, {
  16. 16. 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.tt2Content-Type: text/html; charset=ISO-8859-1<html><head></head><body><h1>Introduction to TT</h2>[% content %]</body></html>s1.tt2Content-Type: text/html; charset=ISO-8859-1<html><head></head>
  17. 17. <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 %] -->
  18. 18. [%# 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>
  19. 19. [% 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>
  20. 20. [% 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 %] -->

×