SlideShare a Scribd company logo
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::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/perl



use strict;

use Template;

my $tt = Template->new({

       INCLUDE_PATH => '../../htdocs/tt2/part1',

       INTERPOLATE => 1,
}) || 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/perl



use strict;

use Template;

my $tt = Template->new({

          INCLUDE_PATH => '../../htdocs/tt2/part1',

          INTERPOLATE => 1,

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

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

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



$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::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
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 & 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::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 & 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::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' },
]

  }

) || 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' },
{ 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::ERRORn";



$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::ERRORn";



$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>&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 %] -->
Code from LPW 2012 Template::Toolkit Workshop

More Related Content

PDF
Unix cheatsheet
PDF
Writing Modular Command-line Apps with App::Cmd
PDF
Does your configuration code smell?
PPT
Expert JavaScript tricks of the masters
PDF
Twig tips and tricks
PDF
Codeware
PDF
Symfony2 revealed
Unix cheatsheet
Writing Modular Command-line Apps with App::Cmd
Does your configuration code smell?
Expert JavaScript tricks of the masters
Twig tips and tricks
Codeware
Symfony2 revealed

Viewers also liked (6)

PDF
Cocreating kenny for the educational market
PDF
Sri krishnaa-techno-systems
PPTX
New jersey
DOCX
Task 3 finished
PDF
Sakai Technical Chinese
PPTX
Ppt praesenz i_dd
Cocreating kenny for the educational market
Sri krishnaa-techno-systems
New jersey
Task 3 finished
Sakai Technical Chinese
Ppt praesenz i_dd
Ad

Similar to Code from LPW 2012 Template::Toolkit Workshop (20)

PPT
Introduction to Template::Toolkit
PDF
Utility Modules That You Should Know About
KEY
Good Evils In Perl (Yapc Asia)
PDF
What's New in Perl? v5.10 - v5.16
PDF
Perl 6 by example
PDF
tutorial7
PDF
tutorial7
ODP
Template Toolkit
PPT
Perl 1997 Perl As A System Glue
PDF
Good Evils In Perl
PDF
I, For One, Welcome Our New Perl6 Overlords
ODP
Modern Perl
PPT
CGI With Object Oriented Perl
ODP
Programming in perl style
PDF
Simple Ways To Be A Better Programmer (OSCON 2007)
PDF
PPTX
Lecture 3 Perl & FreeBSD administration
PDF
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
PDF
Marc’s (bio)perl course
Introduction to Template::Toolkit
Utility Modules That You Should Know About
Good Evils In Perl (Yapc Asia)
What's New in Perl? v5.10 - v5.16
Perl 6 by example
tutorial7
tutorial7
Template Toolkit
Perl 1997 Perl As A System Glue
Good Evils In Perl
I, For One, Welcome Our New Perl6 Overlords
Modern Perl
CGI With Object Oriented Perl
Programming in perl style
Simple Ways To Be A Better Programmer (OSCON 2007)
Lecture 3 Perl & FreeBSD administration
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Marc’s (bio)perl course
Ad

Code from LPW 2012 Template::Toolkit Workshop

  • 1. 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
  • 2. Part 1 s1.cgi #! /usr/bin/perl use 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/perl use strict; use Template; my $tt = Template->new({ INCLUDE_PATH => '../../htdocs/tt2/part1', INTERPOLATE => 1,
  • 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/perl use strict; use Template; my $tt = Template->new({ INCLUDE_PATH => '../../htdocs/tt2/part1', INTERPOLATE => 1, }) || die "$Template::ERRORn";
  • 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/perl use strict; use Template; my $tt = Template->new({ INCLUDE_PATH => '../../htdocs/tt2/part1', INTERPOLATE => 1, }) || die "$Template::ERRORn";
  • 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 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
  • 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 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 %]
  • 7. 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.
  • 8. [% 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::ERRORn"; $tt->process( 's1.tt2', { rows => [ { something => 'apples', something_else => '1 kg' }, { something => 'pears', something_else => '2 kg' } ] } ) || die $tt->error() . "n";
  • 9. 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::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. 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 & 3 kg' }, ] } ) || die $tt->error() . "n";
  • 11. 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::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 & 3 kg' }, ] }
  • 12. ) || 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::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. ] } ) || 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. { 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::ERRORn"; $tt->process( 's6a.tt2', { rows => [ { something => 'apples', something_else => '1 kg', cost => 5.67 },
  • 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. 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>
  • 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. [%# 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. [% 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. [% 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 %] -->