in reply to 1001 CPAN Template modules

Maybe it's best to use an existing implementation as the base interface. I would propose Template Toolkit because it seems to have the most comprehensive API of all templating systems. The "TPD" for Template Toolkit would virtually do nothing other than fowarding method calls to TT directly. Other template systems would need a small wrapper module to translate the TT-like method calls. Generally only a subset of the TT functionality would be supported, but this should be in most cases sufficient.

Here's a quick sketch how this could look like:

#!/usr/bin/perl -w use strict; { package MetaTemplate::TT; use base qw(Template); } { package MetaTemplate::HTML_Template; use HTML::Template; sub new { my $class = shift; bless {}, $class; } sub process { my($self, $input, $vars, $output) = @_; my @ht_args; if (ref $input eq 'SCALAR') { @ht_args = (scalarref => $input); } else { @ht_args = (filename => $input); } my $ht = HTML::Template->new(@ht_args); if ($vars) { while(my($k,$v) = each %$vars) { $ht->param($k => $v); } } my $o = $ht->output; if (ref $output eq 'SCALAR') { $$output = $o; } elsif (defined $output) { open(my $OUT, "> $output") or die $!; print $OUT $o; } else { print $o; } } } { package MetaTemplate::Text_ScriptTemplate; use Text::ScriptTemplate; sub new { my $class = shift; bless {}, $class; } sub process { my($self, $input, $vars, $output) = @_; my $tmpl = Text::ScriptTemplate->new; if (ref $input eq 'SCALAR') { $tmpl->pack($$input); } else { $tmpl->load($input); } if ($vars) { $tmpl->setq(%$vars); } my $o = $tmpl->fill; if (ref $output eq 'SCALAR') { $$output = $o; } elsif (defined $output) { open(my $OUT, "> $output") or die $!; print $OUT $o; } else { print $o; } } } my $t1 = MetaTemplate::TT->new; $t1->process(\<<EOF, { answer => 42 }); The answer is [% answer %]. EOF my $t2 = MetaTemplate::HTML_Template->new; $t2->process(\<<EOF, { answer => 42 }); The answer is <TMPL_VAR NAME=answer>. EOF my $t3 = MetaTemplate::Text_ScriptTemplate->new; $t3->process(\<<'EOF', { answer => 42 }); The answer is <%=$answer%>. EOF

Replies are listed 'Best First'.
Re: Re: 1001 CPAN Template modules
by Anonymous Monk on Apr 22, 2004 at 11:37 UTC

    Bingo!

    Of course one could think more about the API, but i love it already. It is really what i had in mind.

    For maximum flexibility one could add shebang detection:
    my $tpi = TPI->new( dsn => 'tpd:Mason:/my/files/' ); $tpi->register( 'TPD::HTML_Mason', shebang => '#!mason' ); $tpi->register( 'TPD::TT', shebang => '#!tt' ); $tpi->register( 'TPD::HTML_Template', shebang => '#!htmpl' ); $tpi->register( 'TPD::PETAL', shebang => '#!petal' ); $tpi->variables( author => 'a', title => 'b' ); print $tpi->interpolate( <<'END_HERE' ); #!mason Welcome <% $author %> END_HERE print $tpi->interpolate( <<'END_HERE' ); #!htmpl Welcome <TMPL_VAR author> END_HERE
    Of course the TPD driver could translate ie. looop variables n'stuff.

    I am convinced that ~90% demand nothing more than the basic functionality from the templating engine. And so one could unify all the tons of api to just TPI.


    I am convinced this would be fastly adopted/standard as DBI.

    Anybody setting up a ml?

    Murat
      I think if you wait for TT3 (the heir apparent to TT2), you'll find that it does most of what you want. A core of abstraction of the parts of a templating engine, with skins that can be added to mimic most existing systems.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.