Template Toolkit

  • Upload
    dwm042

  • View
    4.866

  • Download
    1

Embed Size (px)

DESCRIPTION

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

Citation preview

  • 1. Template Toolkit

2. Templating

  • Start with a document with some kind of markup to search and replace.

3. Feed document through program 4. Output is as you wanted 5. Fantastic! People started writing their own templating systems, because it increased productivity.

  • document change much easier than code change.

6. Formalized Templating Systems

  • No reinventing the wheel

7. Portability 8. Trainability 9. More extensive features 10. Get someone else to manage the templates

  • other than you

11. Other Templating Systems

  • Text::Template

12. HTML::Template 13. HTML::Mason 14. HTML::Embperl 15. Apache::ASP 16. Template Toolkit

  • Plenty of features

17. Usable through a number of web frameworks 18. Documented on the web

  • http://template-toolkit.org/

Documented via O'Reilly.

  • http://www.amazon.com/Perl-Template-Toolkit-Darren-Chamberlain/dp/0596004761

Usable across languages (Perl and Python implementations) 19. Installing Template Toolkit

  • CPAN module
  • #perl -MCPAN -e 'shell'

20. cpan>install Template 21. #cpan Template It's found in PPM as well. 22. Installed as part of the standard Catalyst installation. 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 n before current line [%- foo %] Post chomp removes n on current line [% foo -%] Both pre and post chomped [%- foo -%] ^_^ 27. Dots dereference components. [% myhash.3.component.7.subcomponent %] 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 -%] 30. Flow of control Part II

  • FOR, FOREACH

31. WHILE 32. NEXT, LAST 33. IF, ELSIF, ELSE, UNLESS 34. SWITCH, CASE 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 party is . ~~~ [%# 2 args to TAGS = start and end directives %] [% TAGS {} %] ~~~ Or in code .. my $tt = Template->new({ START_TAG => quotemeta(''), END_TAG => quotemeta(''), }); 40. WRAPPER option

  • The WRAPPER option alerts the Template object tofirst process the main template and then embed it into a wrapper which is processed later.

41. Template->new({ WRAPPER => 'wrapper.tt' }); 42. Similar functionality is provided by Catalyst. 43. Html tidy can be used to make an initial wrapper.

  • http://tidy.sourceforge.net

44. http://www.paehl.com/open_source/?HTML_Tidy_for_Windows 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!n; } [% 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 %]!n; print I must eat [% entree %] and [% side %].n; [% 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 [% FILTER html %] Lots of C or shell here & this, >> and