23. Using Template Toolkit #! /usr/bin/perl use strict; use Template; my $tt = Template->new(); my $file = “webpage.tt”; my $vars = { person => 'Sam Spade', book => 'Maltese Falcon' }; $tt->process( $file, $vars ) || die $tt->error();
24. Passing Variables to TT Hashes and arrays should be passed as references: $arrayref = [ 1, 2, 3, 4 ]; $hashref = { foo => 1, bar => 2 }; This is also true within Catalyst
25. Simple example template Dear [%- customer -%], We are introducing five new products we want you to consider: [% product.0 %] [% product.1 %] [% product.2 %] [% product.3 %] [% product.4 %]
26. Pre and post chomp No chomp [% foo %] Pre chomp – removes before current line [%- foo %] Post chomp – removes on current line [% foo -%] Both pre and post chomped [%- foo -%] ^_^
28. TT provides virtual methods. In use, looks like Ruby [% string.length %] [% myhash.keys.sort %] [% hashlist.keys.sort.join(', ') %]
29. TT flow of control These teams have the following quarterbacks: [% FOREACH team IN league.keys.sort -%] [% NEXT UNLESS team.defined -%] [% team.name %]: [% team.qb %] [% IF team.owner -%] This team is owned by [% team.owner -%] [% ELSE -%] This team is owned by a committee. [% END -%] [% END -%]
35. Variables within TT [% foo = 7 %] [%# Is shorthand for %] [% SET foo = 7 %] [%# array example %] [% mylist = [1,2,3,4,5] %] [%# hash example %] [% name = { first = 'John' last = 'Dough' } %]
36. Comments within TT [% # comment to end of line # more commentary myname = 7 -%] [%# comment until terminating chars %] The space between the % and the # makes a difference.
37. Process and Include directives [%# These add stuff from external files to your document -%] [% PROCESS stuff %] [%# process acts like a source statement in shell. Any variables added have document level scope. %] [% INCLUDE morestuff %] [%# The include statement, by contrast, keeps those variables local to its own included file %]
38. Using PROCESS [% PROCESS commonheader %] My main web page content [% PROCESS ads %] [% PROCESS commonfooter %]
39. TAGS directive [% TAGS mason %] The <% location > party is <% opinion >. ~~~ [%# 2 args to TAGS = start and end directives %] [% TAGS { } %] ~~~ Or in code .. my $tt = Template->new({ START_TAG => quotemeta(''), END_TAG => quotemeta(''), });
45. META directive [%# Meta allows the inner template to pass information out to a wrapper. %] [% META title = 'This is a math equation' -%]
46. INTERPOLATE flag When set, bare Perl variables can be inserted into text. This is a math equation: $math This flag is off by default.
47. BLOCK directive Useful for simplifying chunks of code that can be included or processed later. [% BLOCK button %] Code for HTML button [% END %] TT has ways to build libraries of BLOCKs.
48. Exception Handling [% TRY %] [% USE DBI(mydb) %] [% CATCH %] Error:: [% error %] [% END %]
49. PERL directive Need to add EVAL_PERL => 1 to constructor to use. [% PERL %] for ( 1 .. 5 ) { print “Hello world!”; } [% END %]
50. PERL directive Template directives within the PERL block are evaluated before the PERL block is. So code like the below is legal. [% PERL %] print “Hello, [% name %] I [% verb %]!”; print “I must eat [% entree %] and [% side %].”; [% END %]
51. RAWPERL directive More efficient, but output restricted to appending to $output variable. Only for TT experts, in general. [% RAWPERL %] Stuff; $output .= “ more data”; [% END %]
52. FILTERS Filters transform data contained with the FILTER block. FILTER html makes text HTML safe <code> [% FILTER html %] Lots of C or shell here & this, >> and << that. [% END %] </code>
53. FILTERS II The | symbol is a shortcut for FILTER and can be used to do inline filtering. [% e = 2.718281828 ; e | format('%0.3f') %]
54. FILTER Examples 1. Convert text to PDF [% FILTER latex(“pdf”) %] Text here [% END %] 2. Add <br /> to all ends of lines [% FILTER html_line_break %] To be or not to be That is the question [% END %]
55. Plugins Implemented via the USE directive. [% USE date %] Today is [% date.format %]. [% calc = date.calc %] [% calc.Monday_of_week(27,2008).join('-') %] [% manip = date.manip %]