• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Template Toolkit
 

Template Toolkit

on

  • 4,696 views

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

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

Statistics

Views

Total Views
4,696
Views on SlideShare
4,696
Embed Views
0

Actions

Likes
1
Downloads
21
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Template Toolkit Template Toolkit Presentation Transcript

    • Template Toolkit
    • Templating
      • 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.
        • document change much easier than code change.
    • Formalized Templating Systems
      • No reinventing the wheel
      • Portability
      • Trainability
      • More extensive features
      • Get someone else to manage the templates
        • other than you
    • Other Templating Systems
      • Text::Template
      • HTML::Template
      • HTML::Mason
      • HTML::Embperl
      • Apache::ASP
    • Template Toolkit
      • Plenty of features
      • Usable through a number of web frameworks
      • 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)
    • Installing Template Toolkit
      • CPAN module
        • #perl -MCPAN -e 'shell'
        • cpan>install Template
        • #cpan Template
      • It's found in PPM as well.
      • Installed as part of the standard Catalyst installation.
    • 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();
    • 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
    • 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 %]
    • 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 -%] ^_^
    • 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.join(', ') %]
    • 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 -%]
    • Flow of control Part II
      • FOR, FOREACH
      • WHILE
      • NEXT, LAST
      • IF, ELSIF, ELSE, UNLESS
      • SWITCH, CASE
    • 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' } %]
    • 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.
    • 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 %]
    • Using PROCESS [% PROCESS commonheader %] My main web page content [% PROCESS ads %] [% PROCESS commonfooter %]
    • 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(''), });
    • WRAPPER option
      • The WRAPPER option alerts the Template object to first process the main template and then embed it into a wrapper which is processed later.
      • Template->new({ WRAPPER => 'wrapper.tt' });
      • Similar functionality is provided by Catalyst.
      • Html tidy can be used to make an initial wrapper.
        • http://tidy.sourceforge.net
        • http://www.paehl.com/open_source/?HTML_Tidy_for_Windows
    • META directive [%# Meta allows the inner template to pass information out to a wrapper. %] [% META title = 'This is a math equation' -%]
    • INTERPOLATE flag When set, bare Perl variables can be inserted into text. This is a math equation: $math This flag is off by default.
    • 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.
    • Exception Handling [% TRY %] [% USE DBI(mydb) %] [% CATCH %] Error:: [% error %] [% END %]
    • PERL directive Need to add EVAL_PERL => 1 to constructor to use. [% PERL %] for ( 1 .. 5 ) { print “Hello world!n”; } [% END %]
    • 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 %]
    • RAWPERL directive More efficient, but output restricted to appending to $output variable. Only for TT experts, in general. [% RAWPERL %] Stuff; $output .= “ more data”; [% END %]
    • 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>
    • FILTERS II The | symbol is a shortcut for FILTER and can be used to do inline filtering. [% e = 2.718281828 ; e | format('%0.3f') %]
    • 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 %]
    • 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 %]
    • Plugins available
      • Autoformat
      • CGI
      • Datafile
      • Directory
      • DBI
      • Dumper
      • File
      • GD
      • Pod
      • String
      • Table
      • URL
      • Wrap
      • XML::DOM
      • XML::RSS
      • XML::Style