SlideShare a Scribd company logo
1 of 21
Download to read offline
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

Viewers also liked

Viewers also liked (6)

Cocreating kenny for the educational market
Cocreating kenny for the educational marketCocreating kenny for the educational market
Cocreating kenny for the educational market
 
Sri krishnaa-techno-systems
Sri krishnaa-techno-systemsSri krishnaa-techno-systems
Sri krishnaa-techno-systems
 
New jersey
New jerseyNew jersey
New jersey
 
Task 3 finished
Task 3 finishedTask 3 finished
Task 3 finished
 
Sakai Technical Chinese
Sakai Technical ChineseSakai Technical Chinese
Sakai Technical Chinese
 
Ppt praesenz i_dd
Ppt praesenz i_ddPpt praesenz i_dd
Ppt praesenz i_dd
 

Similar to Code from LPW 2012 Template::Toolkit Workshop

Introduction to Template::Toolkit
Introduction to Template::ToolkitIntroduction to Template::Toolkit
Introduction to Template::Toolkitduncanmg
 
Shell Script Disk Usage Report and E-Mail Current Threshold Status
Shell Script  Disk Usage Report and E-Mail Current Threshold StatusShell Script  Disk Usage Report and E-Mail Current Threshold Status
Shell Script Disk Usage Report and E-Mail Current Threshold StatusVCP Muthukrishna
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationMohammed Farrag
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2Leandro Lima
 
Smolder @Silex
Smolder @SilexSmolder @Silex
Smolder @SilexJeen Lee
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docxMARRY7
 
R57php 1231677414471772-2
R57php 1231677414471772-2R57php 1231677414471772-2
R57php 1231677414471772-2ady36
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud CastlesBen Scofield
 
Asciidoctor New, Noteworthy and Beyond Devoxx-2017
Asciidoctor New, Noteworthy and Beyond Devoxx-2017Asciidoctor New, Noteworthy and Beyond Devoxx-2017
Asciidoctor New, Noteworthy and Beyond Devoxx-2017Alex Soto
 
Rooted 2010 ppp
Rooted 2010 pppRooted 2010 ppp
Rooted 2010 pppnoc_313
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworksdiego_k
 
Curscatalyst
CurscatalystCurscatalyst
CurscatalystKar Juan
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksNate Abele
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisaujihisa
 

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

Introduction to Template::Toolkit
Introduction to Template::ToolkitIntroduction to Template::Toolkit
Introduction to Template::Toolkit
 
Shell Script Disk Usage Report and E-Mail Current Threshold Status
Shell Script  Disk Usage Report and E-Mail Current Threshold StatusShell Script  Disk Usage Report and E-Mail Current Threshold Status
Shell Script Disk Usage Report and E-Mail Current Threshold Status
 
Tt subtemplates-caching
Tt subtemplates-cachingTt subtemplates-caching
Tt subtemplates-caching
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
 
dplyr
dplyrdplyr
dplyr
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administration
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Workshop on command line tools - day 2
Workshop on command line tools - day 2Workshop on command line tools - day 2
Workshop on command line tools - day 2
 
CakePHP workshop
CakePHP workshopCakePHP workshop
CakePHP workshop
 
Loss Monitor
Loss MonitorLoss Monitor
Loss Monitor
 
Smolder @Silex
Smolder @SilexSmolder @Silex
Smolder @Silex
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
 
R57php 1231677414471772-2
R57php 1231677414471772-2R57php 1231677414471772-2
R57php 1231677414471772-2
 
Building Cloud Castles
Building Cloud CastlesBuilding Cloud Castles
Building Cloud Castles
 
Asciidoctor New, Noteworthy and Beyond Devoxx-2017
Asciidoctor New, Noteworthy and Beyond Devoxx-2017Asciidoctor New, Noteworthy and Beyond Devoxx-2017
Asciidoctor New, Noteworthy and Beyond Devoxx-2017
 
Rooted 2010 ppp
Rooted 2010 pppRooted 2010 ppp
Rooted 2010 ppp
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 

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