Template Toolkit
Templating <ul><li>Start with a document with some kind of markup to search and replace.
Feed document through program
Output is as you wanted
Fantastic! People started writing their own templating systems, because it increased productivity. </li><ul><li>document c...
Formalized Templating Systems <ul><li>No reinventing the wheel
Portability
Trainability
More extensive features
Get someone else to manage the templates  </li><ul><li>other than you </li></ul></ul>
Other Templating Systems <ul><li>Text::Template
HTML::Template
HTML::Mason
HTML::Embperl
Apache::ASP </li></ul>
Template Toolkit <ul><li>Plenty of features
Usable through a number of web frameworks
Documented on the web </li><ul><li>http://template-toolkit.org/ </li></ul><li>Documented via O'Reilly. </li><ul><li>http:/...
Installing Template Toolkit <ul><li>CPAN module </li><ul><li>#perl -MCPAN -e 'shell'
cpan>install Template
#cpan Template </li></ul><li>It's found in PPM as well.
Installed as part of the standard Catalyst installation. </li></ul>
Using Template Toolkit #! /usr/bin/perl use strict; use Template; my $tt = Template->new(); my $file = “webpage.tt”; my $v...
Passing Variables to TT Hashes and arrays should be passed as references: $arrayref = [ 1, 2, 3, 4 ]; $hashref = { foo => ...
Simple example template Dear [%- customer -%], We are introducing five new products we want you to consider: [% product.0 ...
Pre and post chomp No chomp [% foo %] Pre chomp – removes n before current line [%- foo %] Post chomp – removes n on curre...
Dots dereference components. [% myhash.3.component.7.subcomponent %]
TT provides virtual methods.  In use, looks like Ruby [% string.length %] [% myhash.keys.sort %] [% hashlist.keys.sort.joi...
TT flow of control These teams have the following quarterbacks: [% FOREACH team IN league.keys.sort -%] [% NEXT UNLESS tea...
Flow of control Part II <ul><li>FOR, FOREACH
WHILE
NEXT, LAST
Upcoming SlideShare
Loading in …5
×

Template Toolkit

5,308 views

Published on

Beginner's introduction to the Perl Module Template Toolkit. Part of a presentation given at Atlanta Perl Mongers February 2011.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,308
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
24
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Template Toolkit

  1. 1. Template Toolkit
  2. 2. Templating <ul><li>Start with a document with some kind of markup to search and replace.
  3. 3. Feed document through program
  4. 4. Output is as you wanted
  5. 5. Fantastic! People started writing their own templating systems, because it increased productivity. </li><ul><li>document change much easier than code change. </li></ul></ul>
  6. 6. Formalized Templating Systems <ul><li>No reinventing the wheel
  7. 7. Portability
  8. 8. Trainability
  9. 9. More extensive features
  10. 10. Get someone else to manage the templates </li><ul><li>other than you </li></ul></ul>
  11. 11. Other Templating Systems <ul><li>Text::Template
  12. 12. HTML::Template
  13. 13. HTML::Mason
  14. 14. HTML::Embperl
  15. 15. Apache::ASP </li></ul>
  16. 16. Template Toolkit <ul><li>Plenty of features
  17. 17. Usable through a number of web frameworks
  18. 18. Documented on the web </li><ul><li>http://template-toolkit.org/ </li></ul><li>Documented via O'Reilly. </li><ul><li>http://www.amazon.com/Perl-Template-Toolkit-Darren-Chamberlain/dp/0596004761 </li></ul><li>Usable across languages (Perl and Python implementations) </li></ul>
  19. 19. Installing Template Toolkit <ul><li>CPAN module </li><ul><li>#perl -MCPAN -e 'shell'
  20. 20. cpan>install Template
  21. 21. #cpan Template </li></ul><li>It's found in PPM as well.
  22. 22. Installed as part of the standard Catalyst installation. </li></ul>
  23. 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. 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. 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. 26. Pre and post chomp No chomp [% foo %] Pre chomp – removes n before current line [%- foo %] Post chomp – removes n on current line [% foo -%] Both pre and post chomped [%- foo -%] ^_^
  27. 27. Dots dereference components. [% myhash.3.component.7.subcomponent %]
  28. 28. TT provides virtual methods. In use, looks like Ruby [% string.length %] [% myhash.keys.sort %] [% hashlist.keys.sort.join(', ') %]
  29. 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 -%]
  30. 30. Flow of control Part II <ul><li>FOR, FOREACH
  31. 31. WHILE
  32. 32. NEXT, LAST
  33. 33. IF, ELSIF, ELSE, UNLESS
  34. 34. SWITCH, CASE </li></ul>
  35. 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. 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. 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. 38. Using PROCESS [% PROCESS commonheader %] My main web page content [% PROCESS ads %] [% PROCESS commonfooter %]
  39. 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(''), });
  40. 40. WRAPPER option <ul><li>The WRAPPER option alerts the Template object to first process the main template and then embed it into a wrapper which is processed later.
  41. 41. Template->new({ WRAPPER => 'wrapper.tt' });
  42. 42. Similar functionality is provided by Catalyst.
  43. 43. Html tidy can be used to make an initial wrapper. </li><ul><li>http://tidy.sourceforge.net
  44. 44. http://www.paehl.com/open_source/?HTML_Tidy_for_Windows </li></ul></ul>
  45. 45. META directive [%# Meta allows the inner template to pass information out to a wrapper. %] [% META title = 'This is a math equation' -%]
  46. 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. 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. 48. Exception Handling [% TRY %] [% USE DBI(mydb) %] [% CATCH %] Error:: [% error %] [% END %]
  49. 49. PERL directive Need to add EVAL_PERL => 1 to constructor to use. [% PERL %] for ( 1 .. 5 ) { print “Hello world!n”; } [% END %]
  50. 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 %]!n”; print “I must eat [% entree %] and [% side %].n”; [% END %]
  51. 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. 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. 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. 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. 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 %]
  56. 56. Plugins available <ul><li>Autoformat
  57. 57. CGI
  58. 58. Datafile
  59. 59. Directory
  60. 60. DBI
  61. 61. Dumper
  62. 62. File
  63. 63. GD </li></ul><ul><li>Pod
  64. 64. String
  65. 65. Table
  66. 66. URL
  67. 67. Wrap
  68. 68. XML::DOM
  69. 69. XML::RSS
  70. 70. XML::Style </li></ul>

×