Perl Template Toolkit

60 %
40 %
Information about Perl Template Toolkit

Published on October 29, 2007

Author: larsen

Source: slideshare.net

Description

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

Perl Template Toolkit Stefano Rodighiero larsen@perl.it

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;;

Template • Separano il codice applicativo dalla presentazione • Facilitano il riuso del codice • Ci sono n moduli su CPAN • Evidentemente è una buona idea :-)

Template Toolkit • Dal sito statico... • ...al CGI... • ...all’applicazione Web

ABC

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.

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();

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();

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;

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 %]

www.perl.it

Requisiti • Facile rilocabilità • Riuso di elementi già sviluppati • Build locale • Pubblicazione in un solo passo • Riusabilità futura del codice sviluppato

Soluzione • Template Toolkit • ttree • Una manciata di moduli di CPAN • Repository di template su CVS

www.perl.it

www.perl.it

Widget

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 %]

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

ttree

Plugin

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; }

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>

Mastering TT2

Architettura Template Template::Service Template::Provider Template::Context Template::Stash Template::Parser Template::Document Template::Exception

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 %]!

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:~$

Gli stessi princìpi del Perl • Potenza • Semplicità • Disciplina • Scalabilità • Stile

Approfondimenti • http://www.tt2.org • Mailing list • Perl Template Toolkit, O’Reilly • perldoc Template

Grazie :) larsen@perl.it http://larsen.perlmonk.org

Add a comment

Related presentations

Related pages

Template Toolkit Home Page

Next About the Template Toolkit. ... Mailing Lists; Badger Book; Template-Python; ... entitled Perl Template Toolkit but we call it the "Badger Book ...
Read more

Template-Toolkit – Wikipedia

Template-Toolkit ist eine freie, in Perl geschriebene Template-Engine, die seit 1996 hauptsächlich von Andy Wardley entwickelt wird. Dieses CPAN-Modul ...
Read more

Perl Template Toolkit (Classique Us): Amazon.de: Darren ...

Darren - Perl Template Toolkit (Classique Us) jetzt kaufen. ISBN: 0636920004769, Fremdsprachige Bücher - Programmieren
Read more

Template::Toolkit - search.cpan.org

NAME . Template::Toolkit - Template Processing System. Introduction . The Template Toolkit is a collection of Perl modules which implement a fast, flexible ...
Read more

Perl Template Toolkit - O'Reilly Media

Written by core members of the technology's development team, Perl Template Toolkit guides you through the entire process of installing, configuring, using ...
Read more

Template Toolkit Documentation

The Template Toolkit is a collection of Perl modules which implement a fast, flexible, powerful and extensible template processing system.
Read more

Perl Template Toolkit: Darren Chamberlain, Dave Cross ...

Perl Template Toolkit [Darren Chamberlain, Dave Cross, Andy Wardley] on Amazon.com. *FREE* shipping on qualifying offers. Among the many different ...
Read more

Perl Template Toolkit — Cognitiones Publicae

Perl Template Toolkit. Ein Perl-Template-Toolkit-Projekt im TextWrangler. Das Perl Template Toolkit ist eine freie, in Perl geschriebene Template-Engine ...
Read more

Perl Template Toolkit

The Badger Book. Perl Template Toolkit, or the Badger Book, as we like to call it, is a comprehensive guide to using and ...
Read more

Perl: Alternatives to template toolkit - Stack Overflow

How can I profile template performance in Template::Toolkit? 12 ... How can I output perl Template Toolkit examples in Template ToolKit. 0
Read more