Perl Template Toolkit

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Group

    Perl Template Toolkit - Presentation Transcript

    1. Perl Template Toolkit Stefano Rodighiero larsen@perl.it
    2. nothing_particular.pl #!/usr/bin/perl print \"Content-type:text/html\\n\\n\"; print \"<html><head>\"; print \"<title>Print Environment</title></head>\"; print \"<body>”; foreach $key (sort(keys %ENV)) { print \"$key = $ENV{$key}<br>\\n\"; } print \"</body></html>\";
    3. Template • Separano il codice applicativo dalla presentazione • Facilitano il riuso del codice • Ci sono n moduli su CPAN • Evidentemente è una buona idea :-)
    4. Template Toolkit • Dal sito statico... • ...al CGI... • ...all’applicazione Web
    5. ABC
    6. Un semplice esempio [% convenevoli %], per [% festa %] vorrei ricevere [% dove %] un po' di LEGO. Riporto i codici delle confezioni che preferisco: [% FOREACH item IN boxes %] * [% item %] [% END %] [% captatio %] Cordiali saluti, S.
    7. Un semplice esempio/2 my $tt = Template->new(); my $vars = { convenevoli => \"Caro Babbo Natale\" , festa => \"Natale\" , dove => \"sotto l'albero\" , boxes => [ 7190, 897, 6880, 6990 ] , captatio => \"Salutami le renne, eh? Ci tengo.\" }; $tt->process( 'letterina.tt2', $vars ) or die $tt->error();
    8. Un semplice esempio/3 my $tt = Template->new(); my $vars = { convenevoli => \"Cara Befana\" , festa => \"l’Epifania\" , dove => \"nella calza\" , boxes => [ 7190, 897, 6880, 6990 ] , captatio => \"Spero che la schiena vada meglio.\" }; $tt->process( 'letterina.tt2', $vars ) or die $tt->error();
    9. Passare oggetti 1 package Product::DBI; 2 use base 'Class::DBI'; 3 Url::DBI->set_db( Main => 'dbi:mysql:products', 4 '*** LOGIN ***', '*** PASSWORD ***' ); 5 6 package Product; 7 use base 'Product::DBI'; 8 Url->table( 'product' ); 9 Url->columns( All => qw/id description type price/ ); 10 11 package main; 12 13 # ... 14 my $template = Template->new(); 15 my $iterator = Product->retrieve_all(); 16 17 $template->process('list.tt2', { iterator => $iterator }) 18 or die $template->error;
    10. Passare oggetti/2 [% IF iterator.count == 0 %] Nessun prodotto<br /> [% ELSE %] <table> [% WHILE (item = iterator.next) %] <tr> <td>[% item.id %]</td> <td>[% item.description %]</td> <td align=\"right\">[% item.price %]</td> </tr> [% END %] </table> [% END %]
    11. www.perl.it
    12. Requisiti • Facile rilocabilità • Riuso di elementi già sviluppati • Build locale • Pubblicazione in un solo passo • Riusabilità futura del codice sviluppato
    13. Soluzione • Template Toolkit • ttree • Una manciata di moduli di CPAN • Repository di template su CVS
    14. www.perl.it
    15. www.perl.it
    16. Widget
    17. Widget [% WRAPPER wMessage.ttml wTopic = 'Documenti' wImg = 'img/gui/wd.gif' wAlt = 'D:' title = 'Perlfunc' subtitle = 'Funzioni Perl in italiano' icon = 'img/icons/faq.png' url = 'documenti/perlfunc/index.html' %]La documentazione completa delle funzioni Perl in italiano. [% END %]
    18. Configurazione di ttree src = /home/larsen/src/perlit/src dest = /home/httpd/perl.it lib = /home/larsen/src/perlit/lib recurse perl5lib = /home/larsen/src/perlit/lib plugin_base = MyPlugin accept = \\.(html|css|txt|js)$ copy = \\.(gif|jpg|png|pl)$ ignore = \\.(swp)$ pre_process = ./lib/config
    19. ttree
    20. Plugin
    21. MyPlugin::Blog package MyPlugin::Blog; use strict; use warnings; use Template::Plugin; use base qw/ Template::Plugin /; use MT; use MT::Entry; sub new { ... } sub get_entry { my $self = shift; my $entry_id = shift; my $entry = MT::Entry->load( $entry_id ); return $entry; }
    22. MyPlugin::Blog in azione [% USE Blog %] [% entry = Blog.get_entry( entry_id ) %] <tbody> <tr> <td class=\"text\"> <p>[% entry.excerpt %] <a href=\"[% entry.permalink %]\" title=\"Leggi tutto\" target=\"_self\" >Continua</a> </p> </td> </tr> </tbody>
    23. Mastering TT2
    24. Architettura Template Template::Service Template::Provider Template::Context Template::Stash Template::Parser Template::Document Template::Exception
    25. Architettura use Template; use Template::Constants qw/ :debug /; my $tt = Template->new( DEBUG => DEBUG_SERVICE | DEBUG_CONTEXT | DEBUG_PROVIDER ); $tt->process( 'template.tt2', { world => \"Repubblica del Perl\" } ) || die $tt->error(); Hello, [% world %]!
    26. Architettura larsen@alice:~$ perl test.pl [Template::Provider] creating cache of unlimited slots for [ . ] [Template::Service] process(template.tt2, HASH(0x832f75c)) [Template::Context] template(template.tt2) [Template::Context] looking for block [template.tt2] [Template::Context] asking providers for [template.tt2] [] [Template::Provider] _fetch_path(template.tt2) [Template::Provider] searching path: ./template.tt2 [Template::Provider] _load(./template.tt2, template.tt2) [Template::Provider] _compile(HASH(0x832f888), <no compfile>) [Template::Provider] _store(./template.tt2, Template::Document=HASH(0x8412128)) [Template::Provider] adding new cache entry [Template::Service] PROCESS: Template::Document=HASH(0x8412128) [Template::Context] process([ Template::Document=HASH(0x8412128) ], <no params>, <unlocalized>) [Template::Context] template(Template::Document=HASH(0x8412128)) Hello, Repubblica del Perl! larsen@alice:~$
    27. Gli stessi princìpi del Perl • Potenza • Semplicità • Disciplina • Scalabilità • Stile
    28. Approfondimenti • http://www.tt2.org • Mailing list • Perl Template Toolkit, O’Reilly • perldoc Template
    29. Grazie :) larsen@perl.it http://larsen.perlmonk.org

    + Stefano RodighieroStefano Rodighiero, 3 years ago

    custom

    2012 views, 0 favs, 1 embeds more stats

    Template Toolkit 2: basic use, a success case, intr more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 2012
      • 2002 on SlideShare
      • 10 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 34
    Most viewed embeds
    • 10 views on http://www.stefanorodighiero.net

    more

    All embeds
    • 10 views on http://www.stefanorodighiero.net

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Groups / Events