Your SlideShare is downloading. ×
0
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Perl Template Toolkit
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Perl Template Toolkit

3,262

Published on

Template Toolkit 2: basic use, a success case, intro on plugin development

Template Toolkit 2: basic use, a success case, intro on plugin development

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

No Downloads
Views
Total Views
3,262
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
58
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Perl Template Toolkit Stefano Rodighiero larsen@perl.it
  • 2. nothing_particular.pl #!/usr/bin/perl print quot;Content-type:text/htmlnnquot;; print quot;<html><head>quot;; print quot;<title>Print Environment</title></head>quot;; print quot;<body>”; foreach $key (sort(keys %ENV)) { print quot;$key = $ENV{$key}<br>nquot;; } print quot;</body></html>quot;;
  • 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 => quot;Caro Babbo Natalequot; , festa => quot;Natalequot; , dove => quot;sotto l'alberoquot; , boxes => [ 7190, 897, 6880, 6990 ] , captatio => quot;Salutami le renne, eh? Ci tengo.quot; }; $tt->process( 'letterina.tt2', $vars ) or die $tt->error();
  • 8. Un semplice esempio/3 my $tt = Template->new(); my $vars = { convenevoli => quot;Cara Befanaquot; , festa => quot;l’Epifaniaquot; , dove => quot;nella calzaquot; , boxes => [ 7190, 897, 6880, 6990 ] , captatio => quot;Spero che la schiena vada meglio.quot; }; $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=quot;rightquot;>[% 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=quot;textquot;> <p>[% entry.excerpt %] <a href=quot;[% entry.permalink %]quot; title=quot;Leggi tuttoquot; target=quot;_selfquot; >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 => quot;Repubblica del Perlquot; } ) || 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

×